mac python安装第三方库jupyter_macOS下IPython和Jupyter的手动安装模式

本文介绍了在Mac上使用Python2时遇到的Jupyter和IPython版本不兼容问题的解决方法。通过删除Homebrew安装的Jupyter和IPython,然后使用pip分别安装Python2和Python3的IPython版本,确保Python2使用IPython5.x。接着安装Jupyter,使用ipykernel命令创建Python2对应的kernel,最后启动Jupyter Notebook选择正确的kernel,成功运行Python2环境的notebook。
摘要由CSDN通过智能技术生成

如果使用Homebrew,并且安装了最新的ipython和Jupyter的话,会发现这两个包都安装在Cellar目录中。

然后,之前有个项目是Python2的,在运行jupyter notebook的时候,报错如下:ImportError:

IPython 6.0+ does not support Python 2.6, 2.7, 3.0, 3.1, or 3.2.

When using Python 2.7, please install IPython 5.x LTS Long Term Support version.

Beginning with IPython 6.0, Python 3.3 and above is required.

这是部分的,追本溯源,报错是:WARNING:root:kernel fcc73436-10b8-4803-a0af-1a7349f70547 restarted

Traceback (most recent call last):

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 174, in _run_module_as_main

"__main__", fname, loader, pkg_name)

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code

exec code in run_globals

File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/lib/python3.7/site-packages/ipykernel_launcher.py", line 15, in

from ipykernel import kernelapp as app

File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/lib/python3.7/site-packages/ipykernel/__init__.py", line 2, in

from .connect import *

File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/lib/python3.7/site-packages/ipykernel/connect.py", line 13, in

from IPython.core.profiledir import ProfileDir

File "/usr/local/Cellar/jupyter/1.0.0_5/libexec/vendor/lib/python3.7/site-packages/IPython/__init__.py", line 41, in

""")

ImportError:

IPython 6.0+ does not support Python 2.6, 2.7, 3.0, 3.1, or 3.2.

When using Python 2.7, please install IPython 5.x LTS Long Term Support version.

Beginning with IPython 6.0, Python 3.3 and above is required.

See IPython `README.rst` file for more information:

https://github.com/ipython/ipython/blob/master/README.rst

[W 01:47:08.513 NotebookApp] KernelRestarter: restart failed

可以看到,是Python2.7调用了jupyter,而jupyter去调用了ipython,而这个ipython是python3版本的,在python3的包中,甚是诡异。

那么就要想一种ipython多版本的安装方式,让5.x的ipython被python2调用,7.x的被python3调用。怎么实现呢?

我也忘了怎么折腾的了,这是ipython的github和readthedoc:https://github.com/ipython/ipython/blob/master/README.rst​github.comipython/ipythonhttps://github.com/ipython/ipython/blob/master/README.rst​github.comhttps://ipython.readthedocs.io/en/stable/install/index.html#quick-install-reminder​ipython.readthedocs.ioInstallation - IPython 7.0.1 documentationInstallation - IPython 7.0.1 documentation​ipython.readthedocs.io

首先把brew的jupyter和ipython都删了吧,brew remove就行。因为笔者查阅brew的资料没找到解决方案,而查阅的资料可知ipython其实是python(2或者3)包的一部分,也是jupyter web化运行的内核。

brew版本ipython没有了,分别安装pip ipython:

pip install ipython

pip3 install ipython

重复命令可以看到路径:

Requirement already satisfied: ipython in /usr/local/lib/python2.7/site-packages (5.7.0)

查看各自的版本:

python -m IPython --version

>>5.7.0

python3 -m IPython --version

>>7.0.1

那么到这里,两个版本的IPython分别安装到python2和python3的site-packages里面了。

下一步就是安装Jupyter。

pip3 install jupyter

pip install jupyter

IPython readthedoc中说:

The Notebook, nbconvert, and many other former pieces of

IPython are now part of Project Jupyter.

不知道是不是安装了Jupyter就不用安装IPython的意思,但是对于pip和pip3最好两个都install一遍(程序员强迫症)。

关键来了,IPython有个列出kernel的命令:ipython kernelspec list

在列出的kernel中,如果发现目录在python2中的,可以删除(可能就是5.x以上的并不支持python2)

再次确认ipython是5.7版本:

python -m ipykernel --version

>> 5.7.0

安装核:

python -m ipykernel install --name "IPython5.7.0"

>>Installed kernelspec IPython5.7.0 in

/usr/local/share/jupyter/kernels/ipython5.7.0

--name后是取的名字,与默认的Python2区分即可。

然后启动jupyter(这个命令中间用-连接,而之前是空格,对此jupyter的GitHub也没说为什么):

jupyter-notebook

在新建一个notebook后,可以选择kernel,选择之前的python2的话(如果你没有删除),发现kernel过新(超过5.x)的问题依然存在。重启。选择新的核IPython5.7.0后,python2的notebook就可以正常运行了!

The end. ( ̄▽ ̄)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值