3

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!

  • Try replacing app = QgsApplication([],True) with app = QApplication([], True). Then place this after QgsApplication.setPrefixPath(r"/Applications/QGIS.app/Contents/MacOS", True). – Joseph Feb 20 '17 at 10:15
  • I just tried that and got:" NameError: name 'QApplication' is not defined" – cvalenzuela Feb 20 '17 at 16:11
  • now I get this: "AttributeError: 'QApplication' object has no attribute 'initQgis'" – cvalenzuela Feb 20 '17 at 17:00
  • Can't test this as I use Windows but the last thing I will suggest is try replacing QgsApplication.setPrefixPath(r"/Applications/QGIS.app/Contents/MacOS", True) with QgsApplication.setPrefixPath("/Applications/QGIS.app/Contents/Plugins", True) – Joseph Feb 21 '17 at 9:50
  • thanks, but it still does not work! – cvalenzuela Feb 21 '17 at 17:08
2

The following works for me on mac OS Sierra 10.12.6

from qgis.core import *
qgs = QgsApplication([], False)
QgsApplication.setPrefixPath("/Applications/QGIS.app/Contents/MacOS", True)
qgs.initQgis()

print QgsApplication.showSettings() 

I had to move the setPrefixPath after the QgsApplication call

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.