[QGIS-Developer] "failed to load!" - loading vector files into pyQGIS (outside QGIS)

Jonas enok Jonas_enok at hotmail.com
Sat Mar 24 08:07:53 PDT 2018


Hello I am currently trying to learn PyQGIS. At almost the beginning I have encountered a problem loading a shapefile into pyqgis, using a standalone application (in this case, Pyscripter 3.2.2, 32 bit). I have tried both QGIS 3.0 (newest OSGeo4w version) and 2.18 standalone with OSGeo4w applications, with Python 3.6/2.7 respectively, all being 32 bit. The operating system is windows 10 home, 64 bit.

I have tried using several different shapefiles (.shp extensions). They can all be loaded into the different versions of QGIS. I have followed https://docs.qgis.org/2.14/en/docs/pyqgis_developer_cookbook/loadlayer.html#vector-layers , and tried with small variations (changing the path from “/path/to/file.shp” to r”path\to\file.shp”) that GIS stack exchange have suggested.

The current python code is:

  1.  from qgis.core import *
  2.  import qgis.utils
  3.  import os
  4.
  5.  QgsApplication.setPrefixPath('C:/qgis218/apps/qgis-ltr', True)
  6.  qgs = QgsApplication([], True)  #has also tried with False
  7.
  8.  qgs.initQgis()
  9.
  10. data_source = r"C:\python_qgis\airport_tutorial\ne_10m_airports.shp"
  11. layer = QgsVectorLayer(data_source, "airports", "ogr")
  12.
  13. if not layer.isValid():
  14.     print ("Layer failed to load!")
  15. print "File Path points to a file: ", os.path.isfile(data_source)

(the prefix path is correct according to QGIS’ own python console, using print QgsApplication.showSettings() )

The code always returns “layer failed to load!” even though the file path is correct.
I would therefore like to ask what might be the problem here? If more information is needed, please say.
Thanks in advance!
Best regards,
Jonas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20180324/3c99e2fa/attachment.html>


More information about the QGIS-Developer mailing list