1

The full error is:

Traceback (most recent call last):
  File "D:\pyqt4_examples\matplotlib.py", line 4, in <module>
    import matplotlib.pyplot as plt
  File "D:\pyqt4_examples\matplotlib.py", line 4, in <module>
    import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package

The test code is on the file histogram.py:

import sys
for item in sys.path: print( item )

import random
import matplotlib.pyplot as plt

x = random.sample(range(1000), 30)
xbins = [0, len(x)]

print( "x: " + str( x ) )
print( "xbins: " + str( xbins ) )

plt.bar(range(0,30), x)
plt.show()

Steps

After the first time I run the code, I got the python system path double as show bellow when I do for item in sys.path: print( item ):

D:\pyqt4_examples
F:\Python36\python36.zip
F:\Python36\DLLs
F:\Python36\lib
F:\Python36
F:\Python36\lib\site-packages
F:\Python36\lib\site-packages\Sphinx-1.5.6-py3.6.egg
F:\Python36\lib\site-packages\win32
F:\Python36\lib\site-packages\win32\lib
F:\Python36\lib\site-packages\Pythonwin
F:\Python36\lib\site-packages\setuptools-27.2.0-py3.6.egg
D:\pyqt4_examples
F:\Python36\python36.zip
F:\Python36\DLLs
F:\Python36\lib
F:\Python36
F:\Python36\lib\site-packages
F:\Python36\lib\site-packages\Sphinx-1.5.6-py3.6.egg
F:\Python36\lib\site-packages\win32
F:\Python36\lib\site-packages\win32\lib
F:\Python36\lib\site-packages\Pythonwin
F:\Python36\lib\site-packages\setuptools-27.2.0-py3.6.egg
...
Here is the erro message, just above

On the folder F:\Python36\lib\site-packages, is correctly there the matplotlib and matplotlib.pyplot:

$ ls F:\Python36\lib\site-packages\matplotlib
__init__.py               _mathtext_data.py    animation.py       blocking_input.py  ...
_contour.cp36-win32.pyd   _version.py          backend_tools.py   compat             ...
_delaunay.cp36-win32.pyd...                    mathtext.py        pyplot.py...
_image.cp36-win32.pyd     afm.py               bezier.py          contour.py         ...
...

Details

I until yesterday my Anaconda Python 2.7.13 was working wonderfully. But I decided also to install the Python 3.6 to run a new program only for the newer version. And right after that it started not working anymore.

I had not idea why so I uninstalled the Python 3, and still not working. Then I reinstalled Python 2.7 and still not working. I installed the Pythonxy and still not working.

After 4 years without formatting my computer and never deleting my data, as I always used the upgrade tools to migrate from Windows 8.1 to Windows 10, I completely formatted my computer deleting all my data, and installed a fresh clean install of the Windows 10 Creator Update. And installed Python Anaconda 2.7 but still not working. Then I installed Python 3 Anaconda and still not working. Then I uninstalled Python 2.7 and still not working.

I have not Idea why it is not working. How can I debug python, or what can I try to find the problem? I mean, it does not make any sense because it is a fresh system install, how can it immediately broke Python? Either there is been 4 years using python on my old installation and never had problems until yesterday. Now both old and clean fresh install are not working.

My system is now:

$ systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
OS Name:                   Microsoft Windows 10 Pro
OS Version:                10.0.15063 N/A Build 15063

$ python --version
Python 3.6.1 :: Anaconda 4.4.0 (32-bit)

Related questions I could find out:

  1. No module named 'matplotlib.pyplot'; 'matplotlib' is not a package
  2. ImportError: No module named matplotlib with matplotlib installed
  3. ImportError: No module named 'matplotlib.pyplot'; matplotlib is not a package
  4. matplotlib - ImportError: No module named _tkinter
  5. ImportError: No module named matplotlib in IDLE
  6. ModuleNotFoundError: No module named 'matplotlib.pyplot'
  7. Failure to import matplotlib.pyplot in jupyter (but not ipython)
  8. jupyter ModuleNotFoundError: No module named matplotlib
  9. ImportError No module named 'matplotlib'
  10. ImportError: No module named matplotlib
  11. ImportError: No module names 'matplotlib' Python 3.3
  12. ImportError: No module named 'matplotlib' -- Using Anaconda tensorflow environment
  13. Have installed matplotlib 3 times still get error: ImportError: No module named backends.backend_wxagg
  14. anaconda cannot import matplotlib.pyplot
  15. Python matplotlib installation issue
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
  • 1
    Your first `sys.path` is `D:\pyqt4_examples` which has a file called `matplotlib.py`... `import matplotlib.xxx` will try to load this file, which is why it is not a package. Rename it and see if that fixes it. – AChampion Jun 03 '17 at 02:09
  • Thanks @AChampion! It is clearly stated on the error message it is trying to import the file named the same as the module. I cannot believe I lost 2 full days plus future month reinstalling all my apps and backups for something so simple. – Evandro Coan Jun 03 '17 at 02:25

1 Answers1

5

You just need to rename the file matplotlib.py to something other. This error is because Python first looks on the current folder for the file for the import and it is finding that file named as the module. It will never import the modulue correct because it is trying to import the file on your folder.

Everton
  • 66
  • 3
  • 1
    Thanks for the info. Using windows 10 and python 3.6 here. I first checked a wrong file as I normally will not name a file as the package name, so did not notice that. But after a double check, yes, I named the file matplotlib! After changing it, bam, got the plot! – zhihong Mar 21 '18 at 21:28