2016-05-13 31 views
0

私のシステムはUbuntu 15.04 64bitです。 私のPythonのバージョン:jupyter notebook%matplotlibエラー

**:~$ 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 

マイ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. 

は、私が使用していconda install matplotlibconda install numpy、およびconda install scipy。 バージョンと場所ipythonで

# 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[1] %matplotlib 
Using matplotlib backend: Qt4Agg 
In[2] from pylab import* 

は、それはすべての権利です。 jupyterノートブックの新しいpython2するためには

は、私はターミナルでjupyter notebookを入力することで、今Kernels for Python 2

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

で見つかったこれらのコマンドを使用して、python2を新しいです。私は使用しようとする%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 

それは間違っています。

私はmatplotlibscipynumpyをconda環境にインストールします。そして、それはipythonではうまくいくようですが、jupyterノートブックではうまくいかないようです。理由は何ですか?あなたが私にこれを手伝ってもらえれば、とても感謝しています。

+0

ノートブックはあなたが設定した 'ipykernel_py2'環境でコードを実行しています。その環境にmatplotlibをインストールする必要があります。 –

+0

ああ、もう一度ありがとう。今はpyenvを使うことで、大丈夫です。 –

答えて

0

問題が見つかりました。ターミナルで

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は次のとおりです。

['', 
    '/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カーネルを作成するには、次のコードを使用していた覚えています。 websiteの説明では

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スクリプトを新しいものにすることができない理由が他にあるはずです。

最後に、pyenvですべてのピトンの環境を習得する方がよいことを認識しています。私は最初にpyenvをインストールしてから、これらのコマンド、pyenv install anaconda-2.4.0pyenv global anaconda-2.4.0、およびjupyter notebookを使うだけでいいです。

関連する問題