it's my first time using PyQGIS and I'm having trouble loading layers and making everything work. I've installed Qgis properly and I'm able to use the python console, but I'm not able to use the standalone scripts.
If I run this basic example from the docs:
from qgis.core import *
QgsApplication.setPrefixPath("Applications/QGIS.app/Contents/MacOS", True)
qgs = QgsApplication([], False)
qgs.initQgis()
qgs.exitQgis()
I get no errors. But when I try to add a layer - using a shp file from the training_manual_exercise_data just to be sure the data is right, I get that the layer is not valid:
from qgis.core import *
QgsApplication.setPrefixPath("Applications/QGIS.app/Contents/MacOS", True)
qgs = QgsApplication([], False)
qgs.initQgis()
layer = QgsVectorLayer("/data/training_manual_exercise_data/exercise_data/world/continents.shp", "world", "ogr")
if not layer.isValid():
print "Layer failed to load!" # This prints!
qgs.exitQgis()
I've checked that my path is the right one. If I type inside QGis:
QgsApplication.prefixPath()
I get:
Applications/QGIS.app/Contents/MacOS
Which is the path I'm using. I've also tried to getFeatures from the layer just to check if there's something in it but it does not print anything.
I've tried multiple approaches, including this:
import os, sys
sys.path.append('/Applications/QGis.app/Contents/Resources/python')
from qgis.core import *
app = QgsApplication([],True)
QgsApplication.setPrefixPath(r"/Applications/QGIS.app/Contents/MacOS", True)
QgsApplication.initQgis()
layer = QgsVectorLayer('/tmp/myshape.shp', 'test', 'ogr')
print layer.isValid()
But it also does not work. I have also checked that my PATH and PYTHONPATH are correct. I added the Paths to my .bash_profile:
export PATH="/Applications/QGIS.app/Contents/MacOS/bin:$PATH"
export PYTHONPATH="/Applications/QGIS.app/Contents/Resources/python"
And I'm able to import qgis.core or any other modules without errors.
This is what I get when I run QgsApplication.showSettings() from insde Qgis Console:
Application state:
QGIS_PREFIX_PATH env var:
Prefix: /Applications/QGIS.app/Contents/MacOS
Plugin Path: /Applications/QGIS.app/Contents/MacOS/../PlugIns/qgis
Package Data Path: /Applications/QGIS.app/Contents/MacOS/../Resources
Active Theme Name: default
Active Theme Path: /Users/cris/.qgis2///themes/default/icons/
Default Theme Path: :/images/themes/default/
SVG Search Paths: /Applications/QGIS.app/Contents/MacOS/../Resources/svg/
/Users/cris/.qgis2//svg/\nUser
DB Path: /Applications/QGIS.app/Contents/MacOS/../Resources/resources/qgis.db
Auth DB Path: /Users/cris/.qgis2//qgis-auth.db
But when I print it in a standalone script after setPrefixPath I get:
Application state:
QGIS_PREFIX_PATH env var:
Prefix: /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS
Plugin Path: /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/../PlugIns/qgis
Package Data Path: /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/../Resources
Active Theme Name:
Active Theme Path: /Users/cris/.qgis2///themes//icons/
Default Theme Path: :/images/themes/default/
SVG Search Paths: /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/../Resources/svg/
/Users/cristobalvalenzuela/.qgis2//svg/
User DB Path: /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/../Resources/resources/qgis.db
Auth DB Path: /Users/cris/.qgis2//qgis-auth.db
I'm running Python 2.7.13, QGIS 2.18.2 and macOS Sierra 10.12
Any thoughts? This is driving me crazy!
app = QgsApplication([],True)
withapp = QApplication([], True)
. Then place this afterQgsApplication.setPrefixPath(r"/Applications/QGIS.app/Contents/MacOS", True)
.QgsApplication.setPrefixPath(r"/Applications/QGIS.app/Contents/MacOS", True)
withQgsApplication.setPrefixPath("/Applications/QGIS.app/Contents/Plugins", True)