简体   繁体   English

jupyter笔记本%matplotlib错误

[英]jupyter notebook %matplotlib error

My system is Ubuntu 15.04 64bit. 我的系统是Ubuntu 15.04 64位。 My python version: 我的python版本:

**:~$ python
Python 2.7.11 |Anaconda custom (64-bit)| (default, Dec  6 2015, 18:08:32) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2

My ipython version: 我的ipython版本:

**:~$ ipython
Python 2.7.11 |Anaconda custom (64-bit)| (default, Dec  6 2015, 18:08:32) 
Type "copyright", "credits" or "license" for more information.
IPython 4.2.0 -- An enhanced Interactive Python.

I have use conda install matplotlib , conda install numpy , and conda install scipy . 我使用了conda install matplotlibconda install numpyconda install scipy Version and location 版本和位置

# packages in environment at /home/hust/anaconda2:
numpy                     1.10.4                   py27_1  
matplotlib                1.5.1               np110py27_0  
scipy                     0.17.0              np110py27_3

In ipython, 在ipython中,

In[1] %matplotlib
Using matplotlib backend: Qt4Agg
In[2] from pylab import*

It's all right. 没关系。

In order to new a python2 in jupyter notebook, I use these command found in Kernels for Python 2 为了在jupyter笔记本中新建python2 ,我使用在Kernels for Python 2中找到的以下命令

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

Now by entering jupyter notebook in the terminal, and new a python2 . 现在,在终端中输入jupyter notebook ,并新建一个python2 I try to use %matplotlib 我尝试使用%matplotlib

In[1]: %matplotlib
ImportError                         Traceback (most recent call last)
<ipython-input-2-f64cd8484500> in <module>()
----> 1 get_ipython().magic(u'matplotlib')

/home/hust/.local/lib/python2.7/site-packages/IPython       /core/interactiveshell.pyc in magic(self, arg_s)
2161         magic_name, _, magic_arg_s = arg_s.partition(' ')
2162         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2163      return self.run_line_magic(magic_name, magic_arg_s)
2164 
2165      #-------------------------------------------------------------------------

/home/hust/.local/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
2082        kwargs['local_ns'] =     sys._getframe(stack_depth).f_locals
2083        with self.builtin_trap:
-> 2084     result = fn(*args,**kwargs)
2085        return result
2086 

<decorator-gen-106> in matplotlib(self, line)

/home/hust/.local/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
191       # but it's overkill for just that one bit of state.
192       def magic_deco(arg):
--> 193   call = lambda f, *a, **k: f(*a, **k)
194 
195       if callable(arg):

/home/hust/.local/lib/python2.7/site-packages/IPython/core/magics/pylab.pyc in matplotlib(self, line)
98          print("Available matplotlib backends: %s" % backends_list)
99        else:
--> 100     gui, backend = self.shell.enable_matplotlib(args.gui)
101         self._show_matplotlib_backend(args.gui, backend)
102 

/home/hust/.local/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in enable_matplotlib(self, gui)
2937      """
2938      from IPython.core import pylabtools as pt
-> 2939   gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
2940 
2941      if gui != 'inline':

/home/hust/.local/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in find_gui_and_backend(gui, gui_select)
258     """
259 
--> 260 import matplotlib
261 
262     if gui and gui != 'auto':

ImportError: No module named matplotlib

It turns out wrong. 原来是错的。

I do install matplotlib , scipy and numpy in the conda environment. 我确实在scipy环境中安装了matplotlibscipynumpy And it seems work well in the ipython but not in the jupyter notebook. 它似乎在ipython中很好用,但在jupyter笔记本中却不行。 What is the reason? 是什么原因? I will appreciate it very much if you could help me with this. 如果您能帮助我,我将非常感激。

I find the problem. 我找到问题了。

In terminal: 在终端:

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']

But in jupyter notebook, the sys.path is: 但是在jupyter笔记本中,sys.path是:

 ['',
  '/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']

Then I remember I have used the following codes to create a python2 kernel. 然后,我记得我曾使用以下代码创建python2内核。

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

In the website 's description, if you're running Jupyter on Python 3, you can set up a Python 2 kernel like this. 网站的说明中,如果您在Python 3上运行Jupyter,则可以像这样设置Python 2内核。 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. 但是实际上我是在Python 2上运行Jupyter的,所以我不能在jupyter notebook中新建python2脚本的原因应该是其他原因。

Finally, I realize that it is better to master all the pythons' environment via pyenv . 最后,我意识到最好通过pyenv掌握所有python的环境。 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 . 我应该首先安装pyenv,然后只需要使用以下命令, pyenv install anaconda-2.4.0pyenv global anaconda-2.4.0jupyter notebook

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM