[Python] When I tried to use matplotlib for graph drawing, I got the error "this application failed to start because it could not find or load the qt platform plugin" windows "".

problem

test_graph1(A program that graphs a function).py


import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 500)
y = np.sin(x)

plt.figure()
plt.plot(x, y)
plt.show()

When you do the above

This application failed to start because it could not find or load the Qt platform plugin "windows" in "". Reinstalling the application may fix this problem.

Error message appeared and I fell into a state where I could not draw a graph.

environment

Windows 10 Home Python 3.7.7 Visual Studio Code 1.44.2 Anaconda 4.8.3

Solution

From the conclusion, installing Qt, SIP and PyQt5 solved it.

Install Qt open source from here and Execute the following on the console such as the command prompt.

$ pip install SIP
$ pip install PyQt5

WARNING: You are using pip version 19.2.3, however version 20.0.2 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.

If you get a WARNING like the one above, do the following:

$ python -m pip install --upgrade pip

This will install PyQt5 correctly and If you execute test_graph1 (program that draws the graph of the function) .py </ font> again, the graph will be displayed. Figure_1.png This is the error that occurred because the Qt dll was not installed correctly. (For Anaconda environment, it is recommended to install with conda command instead of pip command.)

What I tried until the solution

Even if you can't solve it above, what is written here may be a hint for solving it.

Part 1

Reinstalling the application may fix this problem. </ Font> in the error message I tried to reinstall Anaconda, but it didn't work.

Part 2

After that, when I asked google teacher, this article was caught.

[When I started studying machine learning in Python, I got stuck with "could not find or load the Qt platform plugin windows"] (https://qiita.com/sukoppu1102/items/f5c00b520da0fa5d2ff3)

According to this article, if the environment variables are set correctly, this error will not occur, Setting the environment variables correctly did not help.

Part 3

[[Python] I got an error when I tried to make a tool, so I rebuilt the environment with Anaconda + Pycharm] (https://helicobacterpylori.hatenablog.com/entry/2018/11/03/130000)

For the time being, I solved it by putting the necessary dll in the same folder as the exe. Copy \ platforms under / dist in the folder containing the exe. C:\Users{YOUR_USERNAME}\Anaconda2\lib\site-packages\PyQt5 Copy ** libEGL.dll </ font> ** inside to / dist.

When I went to see the contents of PyQt 5 ** libEGL.dll </ font> ** did not exist in the first place. Isn't PyQt5 installed correctly? I noticed that I installed Qt, PyQt5, and SIP, which are necessary for drawing graphs. In fact, when I looked at the contents of PyQt5 at that time, it was in a pitted state.

Precautions and others

Also, even if the installation of PyQt5 itself is successful

from PyQt5.QtWidget import QWidget, QApplication

When using PyQt5.QtWidget like that For those who are building an environment using Anaconda ImportError </ font>, No Name ‘QtWidgets’ in module ‘PyQt5’ </ font>, etc. There is a high possibility that you will get an error message. In that case, it is effective to try the solution described in this article. This time it is not a graph drawing using QtWidgets I didn't include it in the main story, but it is a must-see for those who use Anaconda.

In my case, I got more errors after following the steps above ... 無題2.png Because it was a statement that it does not correspond to the version of Python you are using Eventually I downgraded the Python version of the virtual environment.

無題.png After that, when I checked the build of the package, it was displayed like this. If you find out how you can solve it other than downgrading I'm thinking of posting an article again. (It's cute that the OS information is out of the build)

Qt, PyQt5, SIP installation details

GUI programming with PyQt5 and python3 [0] I referred to here for the installation described in the solution.