I am writing a standalone script in atom to create a new project and load a layer. The problem is that I wanna use the next command:
vlayer = iface.addVectorLayer('', '', '')
To load layers, then, I first need to import iface from qgis.utils. But, when I run my script in cmd, I get the following error:
from qgis.utils import iface
File "C:\Program Files\QGIS 3.22.0\apps\qgis\python\qgis\utils.py", line 34, in <module>
from qgis.gui import QgsMessageBar
File "C:\Program Files\QGIS 3.22.0\apps\qgis\python\qgis\gui\__init__.py", line 25, in <module>
from qgis._gui import *
RuntimeError: qgis._gui cannot import type '����' from PyQt5.QtCore
I set the path as we can see
and create de PYTHONPATH
I have installed PyQt5 through pip as well. I am out of ideas, can someone please help me?
My code, if it can be of help is:
import os
from qgis.core import *
from qgis.utils import iface
#supply path to qgis install location
QgsApplication.setPrefixPath("C:/PROGRA~1/QGIS32~1.0/apps/qgis", True)
#create a reference to the QgsApplication. setting the second argument to
#False disables the GUI
qgs = QgsApplication([], True)
#load providers
qgs.initQgis()
#get the project instance
project = QgsProject.instance()
#add vector layer
layerPath = r"C:\Users\alvaro.garcia.daroca\OneDrive - Accenture\Documents\Qgis - Daroca\Telefónica - pruebas\Daun\KREIS - DAUN_ORTS.gpkg"
vlayer = iface.addVectorLayer(layerPath, 'Orts', 'ogr')
if not vlayer:
print('Layer failed to load!')
project.write('prueba.qgs')
print('\n END')
qgs.exitQgis()
I have in my computer QGIS-3.22; Python-3.9; Windows 10.
Best Answer
Following the comments above, I found an answer.
Since I installed PyQt5 with pip, I decided to uninstall by typing in my cmd:
pip uninstall PyQt5
Then the error changed to ModuleNotFoundError: No module named 'PyQt5.QtCore'
Then, I went to -> C:\Program Files\QGIS 3.22.0\bin. There I opened qgis-bin.env with a plain text editor, and I copied all the enviroment variables that are underlined,
into my enviroment variables as system variables.
Now everything is ok.