如果使用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.rstgithub.comipython/ipythonhttps://github.com/ipython/ipython/blob/master/README.rstgithub.comhttps://ipython.readthedocs.io/en/stable/install/index.html#quick-install-reminderipython.readthedocs.ioInstallation - IPython 7.0.1 documentationInstallation - IPython 7.0.1 documentationipython.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. ( ̄▽ ̄)