0

I am importing matplotlib from Jupyter Notebook but get the following error.

ImportError: No module named matplotlib

Settings:

Python 2 MacBook
conda: 4.3.30

After run

conda list

it got:

ipykernel 4.6.1
ipython 5.3.0
matplotlib 2.0.2 np112py36_0
matplotlib 2.1.1

Some commends I tried but failed

1.

sudo apt-get install python-matplotlib

and shows

sudo: apt-get: command not found

2.

sudo apt-get install python3-matplotlib

and shows

sudo: apt-get: command not found

commends 1 and 2 are from this site

ImportError: No module named matplotlib with matplotlib installed

3.

pip install matplotlib

and shows

Requirement already satisfied: matplotlib in ./anaconda/lib/python3.6/site-packages Requirement already satisfied: pytz in ./anaconda/lib/python3.6/site-packages (from matplotlib) Requirement already satisfied: cycler>=0.10 in ./anaconda/lib/python3.6/site-packages (from matplotlib) Requirement already satisfied: numpy>=1.7.1 in ./anaconda/lib/python3.6/site-packages (from matplotlib) Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in ./anaconda/lib/python3.6/site-packages (from matplotlib) Requirement already satisfied: six>=1.10 in ./anaconda/lib/python3.6/site-packages (from matplotlib) Requirement already satisfied: python-dateutil>=2.0 in ./anaconda/lib/python3.6/site-packages (from matplotlib)

commend 3 from this website

ImportError: No module named matplotlib.pyplot

Update:

run this

pip2 install matplotlib

it was successfully installed like this

Collecting matplotlib Downloading matplotlib-2.1.1-cp27-cp27m... ... Successfully built subprocess32 Installing collected packages: subprocess32, six, python-dateutil, backports.functools-lru-cache, pyparsing, cycler, numpy, pytz, matplotlib Successfully installed backports.functools-lru-cache-1.4 cycler-0.10.0 matplotlib-2.1.1 numpy-1.13.3 pyparsing-2.2.0 python-dateutil-2.6.1 pytz-2017.3 six-1.11.0 subprocess32-3.2.7

then restart Anaconda and Jupyter, and it was not imported.

1 Answers1

0

By the looks of it you already have matplot lib installed for python3.6, just run with python3.6 instead of python, which ends up running python2.

pip will also normally install packages for python2 so if you want to install them for python3 use pip3 install <package>

since apt-get can not be found you are probably running CentOS which uses yum instead as the default package manager. To install you'd normally use the following command:

yum install <packagename>
mental
  • 836
  • 6
  • 18
  • Thanks for replying! My objective is to install matplotlib for python 2 on Jupyter. So I use pip2 install matplotlib, and the terminal shows Requirement already satisfied, but Jupyter could not import it somehow. Plus, the matplotlib works in text editor. P.S. Yum command not found. – JustAnotherJavaDeveloper Jan 05 '18 at 02:43
  • If your aim is to use python 2 then you need matplotlib installed for python2 and you also need to `pip install jupyter` so that it uses python2 and possibly uninstall it with `pip3` if you installed it there to avoid other possible conflicts. Then try running jupyter and using matplotlib – mental Jan 05 '18 at 03:00
  • @mental , please have a look at this one - https://stackoverflow.com/questions/68789254/modulenotfounderror-no-module-named-mplfinance-errors-out-even-after-pip-st – Dhruva Jain Aug 15 '21 at 06:37