jupyter notebook import error: no module named 'matplotlib'

后端 未结 7 1735
小鲜肉
小鲜肉 2021-01-02 06:50

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

>>>jupyter kernelspec list Available kernels: pyt

相关标签:
7条回答
  • 2021-01-02 07:17

    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.

    0 讨论(0)
  • 2021-01-02 07:24

    When using python3 version of jupyter (pip3 install jupyter), matplotlib has to be installed using pip3: pip3 install matplotlib

    0 讨论(0)
  • 2021-01-02 07:32

    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.

    0 讨论(0)
  • 2021-01-02 07:35

    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 .
    0 讨论(0)
  • 2021-01-02 07:35

    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

    0 讨论(0)
  • 2021-01-02 07:37

    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.

    0 讨论(0)
提交回复
热议问题