Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

matplotlib - Python: "ModuleNotFoundError", but module is installed?

I realize this seems like a generic question, but all answers pointed to having two simultanious python installations - I already uninstalled the other one.

Currently I run my code from PyCharm 2017.1.5 (windows 10) with Python interpreter set as Python 3.6.1 (C:Anaconda3python.exe), i.e. I installed Anaconda3, which includes the matplotlib, and run from PyCharm using the Ananconda3-interpreter.

I've checked in Anaconda Navigator that matplotlib 2.0.2 is installed in the environment.

A minimal (non-working) example:

import matplotlib.pyplot as plt

plt.plot(range(10))
plt.show()

Returns the following error:

C:Anaconda3python.exe C:/Users/John/Documents/CPU/master/untitled/main11.py
Traceback (most recent call last):
  File "C:/Users/John/Documents/CPU/master/untitled/main11.py", line 1, in <module>
    import matplotlib.pyplot as plt
  File "C:Anaconda3libsite-packagesmatplotlibpyplot.py", line 29, in <module>
    import matplotlib.colorbar
  File "C:Anaconda3libsite-packagesmatplotlibcolorbar.py", line 34, in <module>
    import matplotlib.collections as collections
  File "C:Anaconda3libsite-packagesmatplotlibcollections.py", line 37, in <module>
    import matplotlib.lines as mlines
  File "C:Anaconda3libsite-packagesmatplotliblines.py", line 28, in <module>
    from matplotlib.markers import MarkerStyle
ModuleNotFoundError: No module named 'matplotlib.markers'

Process finished with exit code 1

This ran fine 2 weeks ago, but not now. To my knowledge, I didn't change or update anything. The module loads correctly, but it seems to be a change in the module content? If so: How did that happen and how can I fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In my case, I could fix it by setting PYTHONPATH to the path to site-packages folder where the needed packages are located, excluding site-pacages.

I use a pyenv virtual environment, whose path is /home/apk/.pyenv/versions/python-3-7-4. When the environment is activated, pip installs packages to /home/apk/.pyenv/versions/python-3-7-4/lib/python3.7/site-packages. Therefore, in terminal, I set:

$ PYTHONPATH=/home/apk/.pyenv/versions/python-3-7-4/lib/python3.7/

The same should be true for Windows installations of python. If virtual environmets are used, then one could edit activate script to set PYTHONPATH.

After I did it, I checked in a python shell.

$ python
Python 3.7.4 (default, Feb  5 2020, 17:11:33) 
[GCC 5.5.0 20171010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/apk/.pyenv/versions/3.7.4/lib/python37.zip', '/home/apk/.pyenv/versions/3.7.4/lib/python3.7', '/home/apk/.pyenv/versions/3.7.4/lib/python3.7/lib-dynload', '/home/apk/.pyenv/versions/python-3-7-4/lib/python3.7']
>>> sys.executable
'/home/apk/.pyenv/versions/python-3-7-4/bin/python'

Good luck!

References


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...