12

Have found a similar issue, however haven't found proper solution.

Here's a code:

import matplotlib.pyplot as plt
plt.plot([1,2,3],[4,2,5])
plt.show()

Run, got the message:

ImportError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package

I run Linux Mint 18 with preinstalled python-2.7 and python-3.5 (I use python3), before that I was installing modules with a simple sudo apt-get install method and that worked great.

Before running this the code above, I've installed matplotlib in a usual way sudo apt-get install python-matplotlib. As it haven't worked out, started to look for solution.

Python location

which python3 /usr/bin/python3

Current Matplotlib installed

sudo find /usr | grep matplotlib /usr/lib/python3/dist-packages/matplotlib

My tries:

1) I've removed matplotlib with autoremove, and tried to make it sudo apt-get install python3-matplotlib instead. Didn't worked out.

2) Used: pip3 install matplotlib or sudo pip3 install matplotlib. Received errors like:

command python setup.py egg_info failed with error code 1 in /tmp/pip-build- ....

3) Then I found another solution:

sudo apt-get install virtualenv
virtualenv -p /usr/bin/python3 py3env
source py3env/bin/activate
pip install matplotlib

Same outcome.

Haven't tried to use import sys sys.path.append('/usr/lib/pymodules/python2.7/')(proposed in link above), but as I am not sure what exactly this command does (quite a newbie to python and programming itself) - haven't risked.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
emptypocket
  • 201
  • 1
  • 3
  • 8
  • In your virtualenv, when you do `pip install matplotlib`, do you get an error, or does it install successfully? On Ubuntu 14.04 I was able to install and import using the your try #3 example with Python 2 and Python 3 – elethan Dec 28 '16 at 21:46
  • 11
    Check if you have file name `matplotlib.py` in your current directory. – Mike Müller Dec 29 '16 at 22:23
  • 3
    I think Mike Muller has it-- if you happened to name the script you are running "matplotlib" (as I just did), you will see this error. Just rename your script to literally anything else, and it should work fine. –  Jun 28 '17 at 18:31

5 Answers5

35

If you are using pycharm and have matplotlib.py in your current working directory than you get this error. Just delete or rename the matplotlib.py file and it will work.

Milan Kc
  • 351
  • 3
  • 3
5

don't name any file as matplotlib.py within your working directory

  • Two [much](https://stackoverflow.com/a/46261207/354577) [older](https://stackoverflow.com/a/49111409/354577) (over a year) answers say the exact same thing. Please don't repeat answers. – ChrisGPT was on strike May 27 '22 at 22:49
4

In your working directory, check if there is any file matplotlib.py Delete that file and import matplotib again. That should work.

AIT
  • 57
  • 1
  • 1
  • 1
    What does this add to [the answer from six months earlier saying the same thing](https://stackoverflow.com/a/46261207/354577)? Please don't repeat answers. – ChrisGPT was on strike May 27 '22 at 22:50
3

simply install:

python -m pip install -U pip
python -m pip install -U matplotlib
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
Hadad
  • 151
  • 2
  • 7
1

I had the same problem reasons in my case were

  1. python 3.8.2 with matplotlib that I downloaded was for 3.7. Make sure they are the same.

  2. python 64 bits version with matplotlib 32bits. Make sure they are the same

    Use python -m pip install package_which_you_need to install packages for Windows

  3. Make sure to add to PATH the environment variables I forgot to do in my case

  4. pip version was old use Use python -m pip install --upgrade pip to upgrade pip to the latest for Windows

  5. I saved the file name as matplotlib.py. Try to avoid that

  6. Finally I typed matplotlib.pyplot as matplotlib.plyplot remember to check for typos first. The error message looks similar even though I corrected all the steps above.

    ModuleNotFoundError: No module named 'matplotlib.pyplot'

    ModuleNotFoundError: No module named 'matplotlib.plyplot'

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
  • 1
    For me, the reason was that I had saved the file as matplotlib.py and so the problem. It was so obvious but just got overlooked. Thanks ! – Ronak SHAH May 02 '22 at 09:29