PyCharm import error: "matplotlib" claims is not a package, but works successfully in IDLE

Happy october

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

So here is the whole problem. Here is a simple code presented in both pycharm and IDLE:

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

When sent to IDLE, my plot appears. When presenting in pycharm, the following error message appears: During the processing 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 located in the folder of my site packages inside my Python34 folder. Also for further verification:

Install PyCharm

Please help, I was upset, as this is the only module I came across. I looked at StackOverflow and its related websites to help, I made sure that I have all the requirements, etc.

+1
source share
4 answers

I suppose if you named your current Python writer module as matplotlib.py this causes python to load your current writer module instead of the real matplotlib.py , which causes an error.

+8
source

I recommend you use virtualenv . This is not necessary, but is useful for sharing the project environment.

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

Make sure you have the python 3 installation folder specified in the Windows PATH environment variable should be specified if you checked "Add Python 3.5 to PATH" :

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

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

If you do not, you need to add python -m to each of the following commands: python -m <command> , so the command below will be python -m pip install virtualenv . I prefer the first solution.

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

 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 the python 3 virtual environment:

 python_3_env_00/Scripts/activate.bat 

Now you should see the active virtual environment (python_3_venv_00) on your command line, for example:

Now you can install matplotlib :

 pip install matplotlib 

Launch PyCharm and add your own virtual environment when you design the interpreter, go to File->Settings to search for Project Interpreter click the gear icon and Add Local and set the path to your virtual environment, it should look something like this:

Check this:

test

+2
source
 import sys print(sys.path) 

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

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

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

It works?

+1
source

If you still get the error, follow the instructions below: if you use PyCharm, it automatically creates virtualenv. Make sure the script path is set to PATH

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

Then open PyCharm and go to File-> Settings. To search for Project Interpreter, enter a description of the image here.

Click on the settings icon → Existing environment → click ... specify the path below

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

Click Apply → OK, and you're done.

0
source

Source: https://habr.com/ru/post/1259102/


All Articles