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.
QT_QPA_PLATFORM_PLUGIN_PATH
, see here gis.stackexchange.com/a/337623/93097, not an OSGEO one for you, but set a path from progam files..bat
file under QGIS directory (bin ?) that sets the environment variables before launch the QGIS app, so you have "just" to copy the env variables.