0

I know this question has been asked before, but the answer in Opening vector layer in QGIS Python standalone application? doesn't seem to work. i have tried both r"\path\to\file\" and "/path/to/file".

Im currently trying to open a shapefile/vector layer in a pyqgis standalone script. the script:

from qgis.core import *
import qgis.utils
import os

GUIEnabled = True
app = QgsApplication([], GUIEnabled)
app.setPrefixPath("C:/OSGeo4W/apps/qgis", True)
app.initQgis()

data_source = "C:/python_qgis/airport_tutorial/ne_10m_airports.shp"
layer = QgsVectorLayer(data_source, "ne_10m_airports.shp", "ogr")
if not layer.isValid():
    print ("Layer failed to load!")

It always return the "Layer failed to load!" statement. The shapefile has previously been opened in Qgis, and i have checked if the file is valid (it is). Has something been changed in qgis version 3 that affected pyqgis?

  • Did you try adding it using this statement, QgsMapLayerRegistry.instance().addMapLayer(layer) – artwork21 Mar 23 '18 at 11:41
  • I am quite new to PyQGIS, where should it be placed? – Jonas Mar 23 '18 at 11:50
  • Put it after the layer = QgsVectorLayer() statement and see if it adds the layer into Q or not. – artwork21 Mar 23 '18 at 13:07
  • I dont think that works; i have put it on the next line, and it produces the error "QgsMapLayerRegistry is not defined" – Jonas Mar 23 '18 at 16:28
  • QgsMapLayerRegistry has been removed from the qgis api. And there is very little documentation at the moment, especially the python cookbook for qgis isn't up to date. Do you have the possibility to try it in Qgis2 environment? – Andreas Müller Mar 23 '18 at 22:47
1

So, i finally got it working. The biggest change i made was instead of setting up the environment variables in Windows (i.e. PYTHONHOME, PYTHONPATH and PATH), i set them up when starting up my IDE in a .cmd file.

i followed: http://spatialgalaxy.net/2014/10/09/a-quick-guide-to-getting-started-with-pyqgis-on-windows/#ide-example

where it basically sets up the proper PYTHONHOME, PYTHONPATH and PATH. my code (as an example):

@echo off
SET OSGEO4W_ROOT=C:\qgis218
CALL "%OSGEO4W_ROOT%"\bin\o4w_env.bat
CALL "%OSGEO4W_ROOT%"\apps\grass\grass-7.4.0\etc\env.bat
@echo off   
path %PATH%;%OSGEO4W_ROOT%\apps\qgis-ltr\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass-7.4.0\lib


SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis-ltr\python;
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\Lib\site-packages
SET QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis-ltr
SET PATH=C:\qgis218\bin;%PATH%
start  /B "C:\Program Files (x86)\PyScripter\PyScripter.exe" %*

(the different locations have to be changed according to your own needs). admittedly, i dont fully understand why this made it possible.

  • May be that the QGIS_PREFIX_PATH was not set or wrong. But anyway, setting the environment in a batch file ist the way OSGEO4W does it, so following this keeps it consistent. And If you have different Versions of QGIS installed you have to use batch files. – Andreas Müller Mar 25 '18 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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