4
import os, sys
sys.path.append(r'C:\Program Files\QGIS 3.6\apps\qgis\python')
sys.path.append(r'C:\Program Files\QGIS 3.6\apps\qgis\bin')
sys.path.append(r'C:\Program Files\QGIS 3.6\apps\Python37')
sys.path.append(r'C:\Program Files\QGIS 3.6\apps\Python37\Scripts')
sys.path.append(r'C:\Program Files\QGIS 3.6\apps\Qt5\bin')
sys.path.append(r'C:\Program Files\QGIS 3.6\apps\Python27\Scripts')
sys.path.append(r'C:\Program Files\QGIS 3.6\bin')
sys.path.append(r'C:\Windows\System32')
sys.path.append(r'C:\Windows')
sys.path.append(r'C:\Windows\System32\wbem')
sys.path.append(r'C:\Program Files\QGIS 3.6\apps\Python37\Lib\site-packages\pywin32_system32')
sys.path.append(r'C:\Program Files\QGIS 3.6\apps\Python37\Lib\site-packages\numpy\.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

asked Apr 21, 2021 at 14:03
4
  • do you have the line from PyQt5.QtCore import * in your script or does import qgis raise the error? Commented Apr 21, 2021 at 14:47
  • the import qgis raises the error Commented Apr 21, 2021 at 15:02
  • Have you read this already? docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/… Commented Apr 21, 2021 at 15:46
  • yes I did this already and I am still getting the error. Commented Apr 21, 2021 at 16:50

1 Answer 1

5

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 Files\Python37\python.exe) not installed by QGIS I get the same error.

To be sure this is the reason:

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 Apr 21, 2021 at 20:05
5
  • Yeah I know I did this already ! But for me its necessary to get QGIS running in a not QGIS-python-environment, because I want to transfer the standalone application to another computer. Commented Apr 22, 2021 at 6:32
  • Or can you help me writing a batch-file maybe to automatically open the OSGeo4W Shell and run this needed commands ? I just tried doing that, but failed again. Commented Apr 22, 2021 at 10:21
  • and I also get an error, when I try to do it like that, I added the error in my original question ! Commented Apr 22, 2021 at 10:28
  • I don't encounter that error. I have no solution. You can check these search results. There are different solutions for that error in GIS.SE. Commented Apr 22, 2021 at 11:36
  • I already checked every page there :D I really dont know whats going wrong there Commented Apr 22, 2021 at 12:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.