简体   繁体   中英

I installed matplotlib via pip but when I try to import matplotlib to PyCharm I get an error

I'm trying to use matplotlib in python, specifically, in PyCharm IDE. I have Windows 10 and Python 3.8 and I checked that everything was up to date (like pip).

I installed matplotlib via pip with the command "pip install matplotlib", till then everything was okay because I didn't get any error in the cmd and it was successful.

The problem appears when I try to install the package into the project in PyCharm, and an error appears. I haven't found this error on any website and I've tried lots of things, like reinstalling python and PyCharm, installing the library with conda environment, also trying to install updated FreeType but I don't know how to do it...

Also, I noticed that when I import matplotlib in python.exe any error appears, so I thought the error might be in PyCharm, but this is just a speculation. Here's the error:

src/checkdep_freetype2.c(5): fatal error C1189: #error: "FreeType version 2.3 or higher is required. You may set the MPLLOCALFREETYPE environment variable to 1 to let Matplotlib download it."

I don't know how to install an updated FreeType version or setting the MPLLOCALFREETYPE to 1 as the error suggested.

If I can provide more information or more code please let me know, I don't know if I asked "correctly".

Update:

I've tried to write the command also in the terminal of venv and I think I get the same error, but with more detail, it mentions something regarding Microsoft visual studio. Here the error:

IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18
362.0\cppwinrt" /Tcsrc/checkdep_freetype2.c /Fobuild\temp.win32-3.8\Release\src/checkdep_freetype2.obj
    checkdep_freetype2.c
    src/checkdep_freetype2.c(5): fatal error C1189: #error:  "FreeType version 2.3 or higher is required. You may set the MPLLOCALFREETYPE environment variable to 1 to let Matplotlib download
it."
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.25.28610\\bin\\HostX86\\x86\\cl.exe' failed with exit status 2

    ----------------------------------------
Command "C:\Users\Samuel\PycharmProjects\untitled\venv\Scripts\python.exe -u -c "import setuptools, tokenize;__file__='C:\\temp\\pip-install-pzdo9qwp\\matplotlib\\setup.py';f=getattr(tokenize,
 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\temp\pip-record-059s14h2\install-record.txt --single-version-e
xternally-managed --compile --install-headers C:\Users\Samuel\PycharmProjects\untitled\venv\include\site\python3.8\matplotlib" failed with error code 1 in C:\temp\pip-install-pzdo9qwp\matplotl
ib\```

Try:

sudo apt-get install libfreetype6-dev
sudo apt-get install pkg-config
sudo apt-get install libpng12-dev
sudo apt-get install pkg-config

Ref: https://hackaday.io/project/53896-weedinator-2019/log/153140-jupyter-notebook-3-days-to-get-a-photo-of-a-cat

我只是下载了另一个版本的 matplotlib 就解决了这个问题。

python -m pip install -U matplotlib==3.2.0rc1```

I got same problems like you and try to fix it with the error message but not work at all until I do one thing it fixes all problems, "Use python version 3.7"!!! don't use ver3.8 with PyCharm!

在此处输入图片说明

I'm assuming that you just went into the command prompt and typed "pip install matplotlib". This only installs matplotlib for the environment you installed, but pycharm creates its own environment.

So, to install matplotlib, go to the file explorer in pycharm and right click on "venv". Then click open in terminal. Once the terminal opens, you can use pip to install matplotlib, and then everything should work.

This happens to me occasionally. When PyCharm shows that it is installed in the interpreter but still gives an import error, I just create a new project and it imports error-free.

I opened Pycharm as an Administrator so you can use commands and update everything easily. I also typed in terminal:

pip -m install matplotlib

to resolve dependencies and it worked. Also check on settings --> Project interpreter & update every package you can, it worked for me, btw I'm using python 3.8 and had no problems at all.

Python 解释器

In my case I uninstalled pycharm and reinstalled it and upgraded to tha last version. I tried all solutions mentioned above none of them solved problem completley. Problem solved and I run my code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM