1

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

enter image description here

and create de PYTHONPATH

enter image description here

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.

8
  • 1
    It's not advised to install PyQt5 from pip for a PyQGIS use since QGIS comes already with a specific PyQt version and this can be the conflict. Nov 3, 2021 at 10:47
  • I have read about that, but when I uninstalled PyQt5 (pip uninstall PyQt5), and run the script again, I get a ModuleNotFoundError: (ModuleNotFoundError: No module named 'PyQt5.QtCore'), then, what can I do to solve it? Nov 3, 2021 at 10:56
  • 1
    Try to set a 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. Nov 3, 2021 at 11:02
  • I tried to set a QT_QPA_PLATFORM_PLUGIN_PATH as you told me, but the error is still there. To be sure, I went into path variables in windows and I created new system variable as: QT_QPA_PLAT...=C:Program Files\QGIS 3.22.0\apps\Qt5\plugins\platforms. Nov 4, 2021 at 9:26
  • First : try your code under the QGIS Python console, if it works ok. Next step is too look a .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. Nov 4, 2021 at 9:47

2 Answers 2

3

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,

enter image description here

into my enviroment variables as system variables.

enter image description here

Now everything is ok.

0

I had the same error while writing tests for a QGIS Plugin. I used the accepted answer, and it worked for me. Furthermore, I was able to narrow down the exact environmental variable from the list that caused this error, and it was the PYTHONHOME. So I had to SET it before running the unittest command.

Here is how my .bat file looks like:

set PYTHONPATH=c:\Program Files\QGIS 3.22.1\apps\qgis\python
set PYTHONHOME=C:\PROGRA~1\QGIS32~1.1\apps\Python39

python -m unittest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.