notebook: Unable to import installed libraries

In a new conda environment, I run conda install ipython-notebook and then conda install matplotlib. I launch the notebook using jupyter notebook as usual and everything looks okay. However, when I’m trying to import matplotlib (import matplotlib.pyplot as plt or simply import matplotlib) I get ImportError: No module named matplotlib.pyplot and an error in the terminal:

[IPKernelApp] ERROR | No such comm target registered: ipython.widget

The same happens when I install pandas. What did I do wrong? Running conda list shows that both packages are installed in the current environment.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 59 (20 by maintainers)

Most upvoted comments

I changed my kernel spec to just python without the absolute path.

Is sys.executable what you expect?

In [2]: sys.executable
Out[2]: '/home/user/anaconda/envs/jupyter-notebook/bin/python'

That looks okay. I forgot to add that matplotlib and pandas are working from the ipython console, but the qtconsole and the notebook are having this same issue.

UPDATE:

I tried that in the console, which is giving the correct environment. However, running the same in the notebook actually gives the wrong environment. Why? The other environment is not active right now and doesn’t have matplotlib or pandas listed as installed.

Take a look at your kernelspec. The path for calling python will probably point to the wrong environment.

my environment is mac and python3 i also on jupyter can not import matplotlib

on jupyter run sys.executable is ‘/Library/Frameworks/Python.framework/Versions/3.5/bin/python3’ on terminal run sys.executable is ‘/Users/abc/anaconda/bin/python3’

so what can i do to solve the problem ?

and just rm jupyter-kernelspec seemed can not work

The location of the kernel spec depends on your system. Should be in one of these:

from jupyter_core.paths import jupyter_data_dir
print(jupyter_data_dir())

I am sure @minrk can explain this better.

Many thanks. I think it’s sorted now.

the nb_conda extension seems to work. Are there any particular confusions I should watch out for?

This value of sys.executable:

/Users/Chris/anaconda3/bin/python

Means that your Python 3 kernel is running in your conda root environment, not the ipykernel_py3 environment you created. If it was in that environment, it you would see something like /Users/Chris/anaconda3/envs/ipykernel_py3/bin/python.

Oh, okay. In any case, @juhasch’s solution is working perfectly.

Is sys.executable what you expect?

In Ubuntu to find the kernel spec file and change it, in my case: sudo nautilus' to open the folder with root privilege, then navigate to: /usr/local/share/jupyter/kernels/gpu37 then open kernel.json change the wrong path to the correct one. The correct one should be the return by running the correct python version python import sys print(sys.executable)` copy the correct python path to the kernel.js file and save

Hi @takluyver ,

A problem seems to have occured based on this core kernel path issue , I think.

If I run jupyter kernelspec list in root environment, I now get:

Available kernels:
  python3    /Users/Chris/anaconda3/lib/python3.6/site-packages/ipykernel/resources
  python2    /Users/Chris/Library/Jupyter/kernels/python2

and if I run

from sys import executable 
print(executable)  

In Jupyter notebook and python interpreted running on my Python 3 kernel, I get:

/Users/Chris/anaconda3/bin/python

When installing packages etc. I have gone into:

source activate ipykernel_py3

before using conda install to add new packages.

But I am having a problem now with pandas-datareader. I installed using conda install within the ipykernel_py3 environment, with:

conda install -c anaconda pandas-datareader=0.4.0

Which seemed to work, and with conda list , I see:

# packages in environment at /Users/Chris/anaconda3/envs/ipykernel_py3:
#
appnope                   0.1.0                    py36_0  
beautifulsoup4            4.5.3                    py36_0  
cycler                    0.10.0                   py36_0  
decorator                 4.0.11                   py36_0  
freetype                  2.5.5                         2  
icu                       54.1                          0  
ipykernel                 4.6.1                    py36_0  
ipython                   6.0.0                    py36_0  
ipython_genutils          0.2.0                    py36_0  
jupyter_client            5.0.1                    py36_0  
jupyter_core              4.3.0                    py36_0  
libpng                    1.6.27                        0  
matplotlib                2.0.2               np112py36_0  
mkl                       2017.0.1                      0  
numpy                     1.12.1                   py36_0  
openssl                   1.0.2k                        1  
pandas                    0.20.1              np112py36_0  
pandas-datareader         0.4.0                    py36_0    anaconda
path.py                   10.3.1                   py36_0  
pexpect                   4.2.1                    py36_0  
pickleshare               0.7.4                    py36_0  
pip                       9.0.1                    py36_1  
prompt_toolkit            1.0.14                   py36_0  
ptyprocess                0.5.1                    py36_0  
pygments                  2.2.0                    py36_0  
pyparsing                 2.1.4                    py36_0  
pyqt                      5.6.0                    py36_2  
python                    3.6.1                         0  
python-dateutil           2.6.0                    py36_0  
pytz                      2017.2                   py36_0  
pyzmq                     16.0.2                   py36_0  
qt                        5.6.2                         2  
readline                  6.2                           2  
requests                  2.14.2                   py36_0    anaconda
requests-file             1.4.1                    py36_0    anaconda
requests-ftp              0.3.1                    py36_0    anaconda
scipy                     0.19.0              np112py36_0  
seaborn                   0.7.1                    py36_0  
setuptools                27.2.0                   py36_0  
simplegeneric             0.8.1                    py36_1  
sip                       4.18                     py36_0  
six                       1.10.0                   py36_0  
sqlite                    3.13.0                        0  
tk                        8.5.18                        0  
tornado                   4.5.1                    py36_0  
traitlets                 4.3.2                    py36_0  
wcwidth                   0.1.7                    py36_0  
wheel                     0.29.0                   py36_0  
xz                        5.2.2                         1  
zlib                      1.2.8                         3  

But when I try to import pandas_datareader (the correct import name, based on the documentation here within either a Jupyter notebook on the Py3 kernel, or the interpreter import pandas_datareader as pdr

I get ModuleNotFoundError: No module named 'pandas_datareader'

Which I don’t understand.

This install approach has worked for other modules (e.g. seaborn, which seems to be working fine).

Can you help me understand why this module isn’t working, or being recognised?

Many thanks