TransWikia.com

Error "No module named PyQt5.QtCore" in QGIS standalone application

Geographic Information Systems Asked by Moritz Sproll on June 18, 2021

import os, sys
sys.path.append(r'C:Program FilesQGIS 3.6appsqgispython')
sys.path.append(r'C:Program FilesQGIS 3.6appsqgisbin')
sys.path.append(r'C:Program FilesQGIS 3.6appsPython37')
sys.path.append(r'C:Program FilesQGIS 3.6appsPython37Scripts')
sys.path.append(r'C:Program FilesQGIS 3.6appsQt5bin')
sys.path.append(r'C:Program FilesQGIS 3.6appsPython27Scripts')
sys.path.append(r'C:Program FilesQGIS 3.6bin')
sys.path.append(r'C:WindowsSystem32')
sys.path.append(r'C:Windows')
sys.path.append(r'C:WindowsSystem32wbem')
sys.path.append(r'C:Program FilesQGIS 3.6appsPython37Libsite-packagespywin32_system32')
sys.path.append(r'C:Program FilesQGIS 3.6appsPython37Libsite-packagesnumpy.libs')
import qgis

I would like to write a standalone QGIS application. My Python file is on the desktop and I would like to access the QGIS functions. At the beginning I had the error import qgis module not found, which could be solved by the first line of sys.path.append. Now I get the error:

from PyQt5.QtCore import *
ModuleNotFoundError: No module named 'PyQt5.QtCore'

Although I have appended many different paths to the environment variables, I keep getting this error.

How do I solve this error and use the QGIS functions in a standalone application?

Error trying the anwser:
Error

One Answer

You must be using different Python from the installed one by QGIS installation. When I try to run the script using a different Python interpreter (C:Program FilesPython37python.exe) not installed by QGIS I get the same error.

To be sure this is the reason:

  • Add print("DONE!") after the line import qgis in your script above.

  • Save the script as a Python file. (for example, test.py in C:test folder)

  • Open OSGeo Shell.

    enter image description here

  • Run py3_env.

    enter image description here

  • Run python c:testtest.py.

You probably won't get error and it will print DONE!. That means you didn't use the Python interpreter installed by QGIS.

To solve this, either run the .py file in "OSGeo Shell" as mentioned above or set Python environment for the editor you use. For example, you have an option to select a Python interpreter in VSCode editor before running the file.

Answered by Kadir Şahbaz on June 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP