RuntimeError: qgis._gui cannot import type '\ufffd\ufffd\ufffd\ufffd' from PyQt5.QtCore

Viewed 79

i am trying to set a venv to use qgis processing algorithms in a standalone python script. since i am using sublime text, i created a build system and provided the path to python.exe file that gets downloaded with qgis. after creating venv i ran this

from qgis.core import *

but it throwed an error saying that PyQt5.QtCore module is not there. So i activated the venv and installed PyQt5 with the command

pip install PyQt5==5.15.3

. then the above line worked but when i ran the following code

from qgis.core import *
import qgis.utils

it is throwing this error: RuntimeError: qgis._gui cannot import type '\ufffd\ufffd\ufffd\ufffd' from PyQt5.QtCore

perhaps installing PyQt5 separately was a bad idea but because of the first error i had to.

i am on windows and i need to run it on sublime text or jupyter notebook. please provide any sources or solutions that you are aware of. thank you in advance.

2 Answers

It seems that it's not advised to install PyQt5 from pip, because apparently QGIS comes already with a specific PyQt version and this can be the conflict. I think this could be the problem that you have.

I recommend you to look at this other question where your problem is solved.

Basically you have to go to QGIS bin, in Program Files (default), find qgis-bin.env, open it and copy all the underlined enviroment variables into your enviroment variables as system variables.

I hope this helps you, tell if you still have errors and checkout the other question for further information as it is explained step by step with images.

I found a solution finally. instead of manually adjusting paths, i opened python console in qgis and ran the follwing lines

import sys
print(sys.path)

I appended the list i got from above lines to system paths at the beginning of my script. here is the code

import sys
paths = sys.path
qgispaths = ['C:/PROGRA~1/QGIS32~1.0/apps/qgis/./python', 'C:/Users/pamidiashoka/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python', 'C:/Users/pamidiashoka/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python/plugins', 'C:/PROGRA~1/QGIS32~1.0/apps/qgis/./python/plugins', 'C:\\Program Files\\QGIS 3.26.0\\apps\\qgis\\python', 'C:\\Program Files\\QGIS 3.26.0\\apps\\qgis\\python\\plugins', 'C:\\Program Files\\QGIS 3.26.0\\apps\\qt5\\plugins', 'C:\\Program Files\\QGIS 3.26.0\\apps\\gdal', 'C:\\Program Files\\QGIS 3.26.0\\apps\\qt5\\bin', 'C:\\Users\\pamidiashoka\\Documents', 'C:\\Program Files\\QGIS 3.26.0\\bin\\python39.zip', 'C:\\PROGRA~1\\QGIS32~1.0\\apps\\Python39\\DLLs', 'C:\\PROGRA~1\\QGIS32~1.0\\apps\\Python39\\lib', 'C:\\Program Files\\QGIS 3.26.0\\bin', 'C:\\Users\\pamidiashoka\\AppData\\Roaming\\Python\\Python39\\site-packages', 'C:\\PROGRA~1\\QGIS32~1.0\\apps\\Python39', 'C:\\PROGRA~1\\QGIS32~1.0\\apps\\Python39\\lib\\site-packages', 'C:\\PROGRA~1\\QGIS32~1.0\\apps\\Python39\\lib\\site-packages\\win32', 'C:\\PROGRA~1\\QGIS32~1.0\\apps\\Python39\\lib\\site-packages\\win32\\lib', 'C:\\PROGRA~1\\QGIS32~1.0\\apps\\Python39\\lib\\site-packages\\Pythonwin', 'C:/Users/pamidiashoka/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python', 'C:\\Users/pamidiashoka/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python/plugins\\qgis2web', 'C:\\Users\\pamidiashoka\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins']

for i in range(len(qgispaths)):
    if qgispaths[i] not in paths:
        sys.path.append(qgispaths[i])

Now i am able to import all the qgis modules and processing algorithms.

But whenever i run a script that has at least one processing algorithm, it is throwing the following warning/message

QTemporaryDir: Unable to remove "C:\Users\pamidiashoka\AppData\Local\Temp\python-XwLsCo" most likely due to the presence of read-only files.

I am not sure whether i should be concerned about it or not...but i am able to do what i want to.

Related