PyCharm Import Error: Claims 'matplotlib'

2019-07-27 19:08发布

问题:

Happy October everyone,

I've successfully downloaded modules before using either the pycharm installer or pip through the command screen, but for some reason when installing matplotlib pycharm cannot recognize it. I've uninstalled and reinstalled, I've installed through both methods, I've followed past similar questions asked on this site which make sure that you have the same interpreter and that it was installed in the right folder (pycharm error while importing, even though it works in the terminal).

So, here's the whole problem. Here's is the simple code, submitted into both pycharm and IDLE:

import matplotlib.pyplot as plt

plt.plot([1,2,3],[2,1,3])
plt.show()

When submitted into IDLE, my plot appears. When submitted into pycharm, the following error appears: During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/PythonProject/matplotlib.py", line 1, in <module>
    import matplotlib.pyplot as plt
  File "C:\PythonProject\matplotlib.py", line 1, in <module>
    import matplotlib.pyplot as plt
ImportError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package

I am currently running Python 3.4, PyCharm 2016.2.3, and my matplotlib folders are indeed in my site-packages folder inside my Python34 folder. Also for further verification:

PyCharm installation

Please help I've become frustrated since this is the only module I've run into trouble with. I've scoured StackOverflow and related websites to help, I've made sure I have all the requirements, etc.

回答1:

I guess if you named your current writing python module as matplotlib.py.That cause the python load your current writing module instead of the actual matplotlib.py, which triggers an error.



回答2:

I recommend you to use virtualenv. Is not strictly necessary but is good for dividing your project environments.

This is how I tested matplotlib on my Windows 10 installation, hope it helps.

Be sure that you have the python 3 installation folder listed in your Windows PATH environment variable, should be already listed if you checked "Add Python 3.5 to PATH":

You need also to set the Scripts folder in your PATH environment variable usually should be this path:

C:\Users\<your username>\AppData\Local\Programs\Python\Python35\Scripts

If you don't do that you have to prepend python -m to every command below like this: python -m <command>, so the command below would be python -m pip install virtualenv. I prefer the first solution.

To test matplotlib on Pycharm I've used virtualenv, here is how; first install virtualenv:

pip install virtualenv

Then you create your virtual environment in a folder of your choice, in my case I used python_3_env_00:

virtualenv python_3_env_00

After that you can activate you python 3 virtual environment:

python_3_env_00/Scripts/activate.bat

Now you should see in your command line the active virtual environment (python_3_venv_00), like this:

Now you can install matplotlib:

pip install matplotlib

Fire up PyCharm and add your virtual environment as you project interpreter, go to File->Settings search for Project Interpreter click on the gear icon and Add Local and set the path of your virtual environment, should look like something like this:

Test it:



回答3:

import sys
print(sys.path)

run this code in where the import worked, and run it in the Pycharm project. Compare the lists. Find out which path that is not present in Pycharm sys.path.

Before importing pyplot, append the missing path to sys.path.

import sys

sys.path.append("the path")
import matplotlib.pyplot as plt

Does this work?



回答4:

Please follow below steps if you are still getting an error: If you are using PyCharm, it automatically create virtualenv. Ensure Scripts path is set into PATH

C:\Users\<Username>\AppData\Local\Programs\Python\Python37-32

Then open PyCharm and go to File-> settings. Search for Project Interpreter. You will see window like this sample image

Click on setting icon -> Existing Environment -> click on ... give below path

C:\Users\Krunal\AppData\Local\Programs\Python\Python37-32\python.exe

Click on Apply -> ok and you are good to go.