Jupyter笔记本,ImportError:没有名为pylab的模块

问题描述:

我是ubuntu用户,并且已经在计算机上安装了python 2.7.9. 为了使用jupyter笔记本,我下载了anaconda2. 安装后,我发现输入命令"jupyter notebook"后,浏览器将打开jupyter notebook,但找不到新的python2,因此我转到了

I am a ubuntu user, and I have install python 2.7.9 on my computer. In order to use jupyter notebook, I down load anaconda2. After my installation, I find after entering the command 'jupyter notebook', my browser will open jupyter notebook, but I can't find new python2, so I went to the https://ipython.readthedocs.org/en/latest/install/kernel_install.html Use the following commands:

conda create -n ipykernel_py2 python=2 ipykernel
source activate ipykernel_py2    
python -m ipykernel install --user

现在我可以在jupyter笔记本上新建python2了,但是当我输入命令'from pylab import *'时 它将错误显示为ImportError:没有名为pylab的模块 但是,如果我打开终端并输入"python",然后输入"from pylab import *",则一切正常.

Now I do can new python2 on the jupyter notebook, but when I enter the command 'from pylab import *' it will show the error as ImportError: No module named pylab but if I open a terminal and enter 'python' and then enter 'from pylab import *', it works all ok.

那么,什么问题呢?我是python和jupyter笔记本的新手. 非常感谢.

So, what is the problem? I am new to python and jupyter notebook. Thank you very much.

我找到了问题.

在终端中:

ipython
In [1]: import sys

In [2]: sys.path
Out[2]: 
['',
 '/home/hust/anaconda2/bin',
 '/home/hust/anaconda2/lib/python27.zip',
 '/home/hust/anaconda2/lib/python2.7',
 '/home/hust/anaconda2/lib/python2.7/plat-linux2',
 '/home/hust/anaconda2/lib/python2.7/lib-tk',
 '/home/hust/anaconda2/lib/python2.7/lib-old',
 '/home/hust/anaconda2/lib/python2.7/lib-dynload',
 '/home/hust/.local/lib/python2.7/site-packages',
 '/home/hust/anaconda2/lib/python2.7/site-packages',
 '/home/hust/anaconda2/lib/python2.7/site-packages/Sphinx-1.3.5-py2.7.egg',
 '/home/hust/anaconda2/lib/python2.7/site-packages/setuptools-20.3-py2.7.egg',
 '/home/hust/.local/lib/python2.7/site-packages/IPython/extensions',
 '/home/hust/.ipython']

但是在jupyter笔记本中,sys.path是:

But in jupyter notebook, the sys.path is:

 ['',
  '/home/hust/anaconda2/envs/ipykernel_py2/lib/python27.zip',
  '/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7',
  '/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7/plat-linux2',
  '/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7/lib-tk',
  '/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7/lib-old',
  '/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7/lib-dynload',
  '/home/hust/.local/lib/python2.7/site-packages',
  '/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7/site-packages/setuptools-20.7.0-py2.7.egg',
  '/home/hust/anaconda2/envs/ipykernel_py2/lib/python2.7/site-packages',
  '/home/hust/.local/lib/python2.7/site-packages/IPython/extensions',
  '/home/hust/.ipython']

那我记得我曾经使用以下代码创建python2内核.

Then I remember I have used the following codes to create a python2 kernel.

conda create -n ipykernel_py2 python=2 ipykernel
source activate ipykernel_py2    
python -m ipykernel install --user

网站的说明中(如果正在运行)在Python 3上的Jupyter上,您可以像这样设置一个Python 2内核.但是实际上我是在Python 2上运行Jupyter的,所以我不能在jupyter笔记本中新建python2脚本的原因应该是其他原因.

In the website's description, if you’re running Jupyter on Python 3, you can set up a Python 2 kernel like this. But actually I am running Jupyter on Python 2, the reason why I could not new a python2 script in jupyter notebook should be something else.

最后,我意识到最好通过 pyenv 来掌握所有python的环境.我应该首先安装pyenv,然后只需要使用以下命令pyenv install anaconda-2.4.0pyenv global anaconda-2.4.0jupyter notebook.

Finally, I realize that it is better to master all the pythons' environment via pyenv. I should firstly install pyenv and then I only need use these commands, pyenv install anaconda-2.4.0 , pyenv global anaconda-2.4.0 and jupyter notebook.