Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import of package works in IPython shell but not in Jupyter notebook

I think this question is probably closely related to another one I had, but I'm not sure what the best general-purpose answer is.

On my laptop, if I log into the IPython shell, I can execute

In [1]: import matplotlib

without error.

But if I try to do the same thing in a Jupyter notebook, I get the following error:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-82be63b7783c> in <module>()
----> 1 import matplotlib

ModuleNotFoundError: No module named 'matplotlib'

What explains this? What should I do to fix this? Why does the IPython shell have access to a package that Jupyter notebook doesn't have access to?

like image 338
dbliss Avatar asked Dec 10 '25 16:12

dbliss


2 Answers

It seems to me that the problem that you encountered is not related to the package actually, you may be facing the problem if the environments that you work on Jupyter and IPython from each other.

First thing you can do is to check whether the environment is run commands:

which python3

and

which jupyter

commands on your Terminal. You can then see whether they show the same environment.

Another problem can be that the package "matplotlib" is not installed to the environment that you work on Jupyter, assuming that you use Anaconda. Check whether you have installed the package to the environment that you actually run on Anaconda.

like image 121
Onur Izmitlioglu Avatar answered Dec 13 '25 05:12

Onur Izmitlioglu


As mentioned in earlier answers, it appears that jupyter is using a different python interpreter than the one you are getting when starting a python shell. For a little explanation of it;
In Linux all python installs are in /usr/bin with python3 being a symlink to the maintained version of python3.{SUBVERSION-NUMBER} (and python to python2 which in turn is linked to the maintained version of python2.{SUBVERSION-NUMBER})
for example on my machine

$ ll /usr/bin/python*
lrwxrwxrwx 1 root root    7 Mar  4 10:48 /usr/bin/python -> python2*
lrwxrwxrwx 1 root root    9 Mar  4 10:48 /usr/bin/python2 -> python2.7*
-rwxr-xr-x 1 root root 3.6M Apr  5 21:42 /usr/bin/python2.7*
lrwxrwxrwx 1 root root    9 Mar 26 06:25 /usr/bin/python3 -> python3.7*
-rwxr-xr-x 2 root root 4.4M Oct 22  2018 /usr/bin/python3.6*
-rwxr-xr-x 2 root root 4.4M Oct 22  2018 /usr/bin/python3.6m*
-rwxr-xr-x 2 root root 4.7M Apr  3 01:39 /usr/bin/python3.7*
lrwxrwxrwx 1 root root   33 Apr  3 01:39 /usr/bin/python3.7-config -> x86_64-linux-gnu-python3.7-config*
-rwxr-xr-x 2 root root 4.7M Apr  3 01:39 /usr/bin/python3.7m*
lrwxrwxrwx 1 root root   34 Apr  3 01:39 /usr/bin/python3.7m-config -> x86_64-linux-gnu-python3.7m-config*
lrwxrwxrwx 1 root root   16 Mar 26 06:25 /usr/bin/python3-config -> python3.7-config*
lrwxrwxrwx 1 root root   10 Mar 26 06:25 /usr/bin/python3m -> python3.7m*
lrwxrwxrwx 1 root root   17 Mar 26 06:25 /usr/bin/python3m-config -> python3.7m-config*

running python3.7 will give you a python3.7 shell just as running python3. all their pre-installed packages will be in the corresponding /usr/lib/python{VERSION-NUMBER}/dist-packages
and the user installed packages will be in ~/.local/lib/python{VERSION-NUMBER}/site-packages
and if you are running python from a virtual environment created using venv the packages will be in {VENV-FOLDER}/lib/python{PYTHON_VERSION_USED_TO_CREATE_ENV}/site-packages.
As you mentioned, checking out sys.path in you python shell will show you where the interpreter is looking for packages

>>> import sys
>>> sys.path
['',
 '/usr/lib/python37.zip',
 '/usr/lib/python3.7',
 '/usr/lib/python3.7/lib-dynload',
 '/home/{USER}/.local/lib/python3.7/site-packages',
 '/usr/local/lib/python3.7/dist-packages',
 '/usr/lib/python3/dist-packages']

using a shell from a virtual env venv in this case, sys.path looks like this

>>> sys.path
['',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/home/{USER}/{PATH-TO-VENV}/lib/python{PYTHON_VERSION_USED_TO_CREATE_ENV}/site-packages']

the first entry '' is the directory of __file__ (which is blank in a repl shell) thus enabling you to import modules from the same folder you are running the script from without you having to install your own folder first.

like image 29
Zubda Avatar answered Dec 13 '25 06:12

Zubda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!