One of the related questions mentions an issue with import order, but I don't think that applies here because this is a command-line script: I'm not using Qt, only QGIS libraries.
I'm trying to run the following:
import sys from qgis.core import QgsApplication, QgsVectorLayer sys.path.append("/Applications/QGIS.app/Contents/Resources/python") QgsApplication.setPrefixPath("/Applications/QGIS.app/", True) qgs = QgsApplication(, False) qgs.initQgis() layer = QgsVectorLayer("/path/to/file.shp", "layer", "ogr") layer.isValid() # False for field in layer.fields(): print field # Nothing prints qgs.exitQgis()
Assuming the path exists and points to a valid ESRI shapefile, any ideas as to why this fails to load correctly? Does it need to be explicitly loaded into the QgsApplication context? Is there something else I'm missing?
Environment: macOS Mojave, QGIS 2.18, Python 2.7.