pythonpycharmpyqt5anaconda3

How to resolve "no Qt Platform Plugin could be initialized" for a Qt5 application?


I am working on a python application based on PyQt5. Everything was running good until I redo my PC and reinstall windows again because of some issue. I had copied my environment and after reinstalling Anaconda, I copied that environment again in env folder. Now the problem is that, when I run my code in PyCharm IDE, it displays and error dialog like this:

This is the Error Dialog

I have tried multiple solutions such as:

Solution 1: Change the QT Files Location

Sometimes, a simple trick such as changing the QT files location is enough to get rid of the error. Here’s how you can do it:

  1. Launch File Explorer and open This PC.
  2. Using the Search field, search for pyqt5_tools.
  3. When Windows finishes the search, right-click the pyqt5_tools and head to Open folder location.
  4. Head to PyQt5 > Qt > bin. Copy the platforms folder. in my case bin is not available in this folder
  5. Make a new search for site-packages and open the folder.
  6. There, paste the platforms folder.
  7. Windows will warn you there’s already a folder with the same name. Click Replace the files in the destination.

Solution 2: Run an SFC Scan

There’s a chance Windows display the “Application failed because no QT platform plugin could be initialized” error due to corrupt system files. Fortunately, Windows has a built-in tool to help you fix the problem.

In the Start menu search bar, search for command prompt and select Run as administrator. Then, run the sfc /scannow command line. Windows will scan and automatically replace any corrupted system file.

Solutions Reference

Nothing worked for me, I'm getting the same error all the time. How can I resolve the error displayed in the figure above?

Any help will be appreciated, Thanks.


Solution

  • Updated Answer to this question

    Answer Reference

    To add PyQt5's library path to your PATH environment variable:

    1. Open the Edit Environment Variables dialog.

    2. Select the appropriate Path variable (either User or System variables).

    (Note: I have changed in system variables)

    3. Edit the variable and add the PyQt5 library path as three entries at the top.

    4. Add the following 3 entries, and make sure the entries are in the desired order and at the top of path variable.

    5. Save the changes.

    C:\ProgramData\Anaconda3\envs\ann_tool\Lib\site-packages\PyQt5\Qt5\bin
    C:\ProgramData\Anaconda3\envs\ann_tool\Lib\site-packages\PyQt5\Qt5\plugins
    C:\ProgramData\Anaconda3\envs\ann_tool\Lib\site-packages\PyQt5\Qt5\plugins\platforms
    

    6. Also, if you have a variable named: QT_PLUGIN_PATH, you want to put this line at the top of the list:

    C:\ProgramData\Anaconda3\envs\ann_tool\Lib\site-packages\PyQt5\Qt5\plugins
    

    (Note: In my case, Anaconda is installed for all users so, Anaconda will be found in C:\ProgramData)

    7. Once you have saved the changes to your environment variables, it is necessary to Restart PyCharm in order for it to recognize and locate the updated libraries..