简体   繁体   中英

jupyter notebook import error: no module named 'matplotlib'

I'm an ubuntu 16.4 user and I installed anaconda3 and using both python2 and python3 kernels.

>>>jupyter kernelspec list Available kernels: python2 /home/peterkim/.local/share/jupyter/kernels/python2 python3 /home/peterkim/anaconda3/share/jupyter/kernels/python3

and.. the problem was that I don't know where/how to install packages in order to for my python2 jupyter notebook not to make error 'no module named...'. I tried pip install matplotlib and conda install matplotlib and I also appended '/home//anaconda2/pkgs' to the sys.path .

(I also installed anaconda2 in search of the way of using parallel kernels. After I realised that anaconda2 was not needed. but I didn't uninstall it.)

截屏

使用 python3 版本的 jupyter ( pip3 install jupyter ) 时,必须使用pip3 install matplotlibpip3 install matplotlib

I'd recommend reading through here:

https://conda.io/docs/py2or3.html

I had the same problem after installing a Python 2.7 environment inside my Anaconda3 installation. I'm not sure which command I used to create the environment, but it actually didn't install all the optional packages that are usually bundled with Anaconda (like matplotlib, numpy, ...). But that's not a big issue, you can easily install additional packages with pip as follows:

First, list your conda environments:

C:\Users\Felix>conda info --envs
# conda environments:
#
ipykernel_py2            D:\Anaconda\envs\ipykernel_py2
root                  *  D:\Anaconda

Next, activate your python 2 environment using source activate <env> (Linux/OSX) or activate <env> (Windows):

C:\Users\Felix>activate ipykernel_py2

(ipykernel_py2) C:\Users\Felix>

Finally, use pip to install the required packages:

(ipykernel_py2) C:\Users\Felix>pip install matplotlib
Collecting matplotlib
...
Successfully installed matplotlib-2.0.0

Hope that this helps.

For those still looking for a solution, especially using virtualenv, this worked for me:

1 - Inside your project directory, create a virtual environment. You may have to install virtualenv in case you don't have it

virtualenv myenv --python=python3.7

2 - Install matplotlib inside of your virtual env:

pip3 install matplotlib

3 - Install ipykernel inside your virtual env

pip3 install ipykernel

4 - Connect your jupyter kernel to your new environment. You may have to use sudo here

python3 -m ipykernel install --name=myenv

5 - When you start your jupyter lab , you will have the option to select your env, which has matplotlib installed

在此处输入图片说明

I got around it with typing the following command in the terminal:

conda install matplotlib

This will download the matplotlib package into your anaconda directory.

This worked for me on my windows 10 :

  1. I didn't use conda. I simply downloaded python 3.x version, then created a python 3 environment by the following command : c:\\python3x\\python -m venv c:\\path\\to\\your\\env .
  2. After that you can check your python version by this command python -v .
  3. Then you need to activate the python 3 environment by entering this command : env/Scripts/activate .
  4. Then install the matplotlib library by doing pip3 install matplotlib .

I have checked the version of python executable and the path from where the library is getting used:

import sys

sys.executable # to know the version of executable used

sys.path # to know from which path library is getting imported.

And then: I have installed the library in jupyter notebook cell by using pip.

pip install matplotlib

After that import started working for me.

In my case, the matplotlib conda pkg was corrupted.

conda list

First, identify all the matplotlib pkgs installed in your environment. In my case, there were 2 pkgs.

matplotlib

matplotlib-base

Now remove those using conda.

conda remove matplotlib
conda remove matplotlib-base

Now check the list again to make sure, all the pkgs removed successfully. Then reinstall them again.

conda install matplotlib
conda install matplotlib-base

You may encounter an error saying

SafetyError: The package for matplotlib-base located at /home/<yourusername>/anaconda3/pkgs/matplotlib-base-3.1.3-py37hef1b27d_0 appears to be corrupted. The path 'lib/python3.7/site-packages/matplotlib-3.1.3-py3.7-nspkg.pth' has an incorrect size. reported size: 569 bytes actual size: 570 bytes

Now you need to remove this corrupted folder, in my case, "matplotlib-base-3.1.3-py37hef1b27d_0".

Then try installing the pkgs again. It's better to run

conda remove matplotlab

again before reinstalling, to make sure anything left of those pkgs completely wiped out.

I'm an ubuntu 16.4 user and I installed anaconda3 and using both python2 and python3 kernels.

>>>jupyter kernelspec list Available kernels: python2 /home/peterkim/.local/share/jupyter/kernels/python2 python3 /home/peterkim/anaconda3/share/jupyter/kernels/python3

and.. the problem was that I don't know where/how to install packages in order to for my python2 jupyter notebook not to make error 'no module named ...'. I tried pip install matplotlib and conda install matplotlib and I also appended '/home//anaconda2/pkgs' to the sys.path .

(I also installed anaconda2 in search of the way of using parallel kernels. After I realised that anaconda2 was not needed. but I didn't uninstall it.)

截屏

In windows OS, I found @melkorCba suggestion helpful. But, I have some edits:

  1. After you type conda list in conda console, you can see matplotlib listed. Do not uninstall the module yet.
  2. Go to conda GUI, launch Jupyter, clear Kernel and run the notebook again. To clear Kernel, in the Jupyter notebook, go to kernel tab > Restart and Clear Output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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