I am trying to use the QGIS functions in a stand alone Python script. I'm running Mountain Lion OS, 2.7 Python & the QQGISis 2.0.1 Dufour.

My main problem is that I cannot get started - I cannot load a simple shapefile in order to buffer it. After searching the online resources quite a bit, I actually found that many people had a problem with setting the prefix path, so I ran the QgsApplication.showSettings() to identify it.

The code I'm running now is:

 QgsApplication.setPrefixPath("/Applications/QGIS.app/Contents", True)  
    print QgsApplication.showSettings()        
    #Loading a Layer  
    layer = QgsVectorLayer('/path/to/shapefile_folder/test.shp', 'test', 'ogr')
    if not layer.isValid():  
        print "The layer failed to load."          
    #finally I d like to apply a buffer, sth like that maybe? 
    buffer_Shp = layer.buffer(10, 5) 

Here is the result I get:

 Application state:
    QGIS_PREFIX_PATH env var:       
    Prefix:     /Applications/QGIS.app/Contents
    Plugin Path:        /Applications/QGIS.app/Contents/../PlugIns/qgis
    Package Data Path:  /Applications/QGIS.app/Contents/../Resources
    Active Theme Name:  
    Active Theme Path:  :/images/themes//
    Default Theme Path: :/images/themes/default/
    SVG Search Paths:   /Applications/QGIS.app/Contents/../Resources/svg/
    User DB Path:   /Applications/QGIS.app/Contents/../Resources/resources/qgis.db

"The Layer Failed to load" (obviously)

bash_profile (in case that sth is wrong there)

 /# Setting PATH for Python 2.7
    /# The orginal version is saved in .bash_profile.pysave
    /######export PYTHONPATH=/usr/local/lib/python:$PYTHONPATH
    export PATH=/usr/local/bin:$PATH
    export PATH=/usr/local/share/python:$PATH    
    /#adding Qgis libs  
    export DYLD_LIBRARY_PATH=/Applications/QGIS.app/Contents/MacOS/lib/:/Applications/QGIS.app/Contents/Frameworks/
    export PYTHONPATH=/Applications/QGIS.app/Contents/Resources/python/
    export PATH=/Library/Frameworks/GDAL.framework/Programs:$PATH
    /# Added by Canopy installer on 2013-10-06
    /# VIRTUAL_ENV_DISABLE_PROMPT can be set to '' to make bashprompt show that Canopy is active, otherwise 1
    /# VIRTUAL_ENV_DISABLE_PROMPT=1 source /Users/george/Library/Enthought/Canopy_64bit/User/bin/activate

What the heck am I missing?

1 Answer 1


There are a couple of things you may want to look at concerning your code:

  1. If you are actually trying to load the shapefile from '/path/to/shapefile_folder/test.shp' then there is a high likelihood that a shapfile does not exist at that location.

  2. Set the prefixPath to '/Applications/QGIS.app/Contents/MacOS'. Notice if you look inside the application bundle, the following path from your QgsApplication.showSettings() is invalid: /Applications/QGIS.app/Contents/../PlugIns/qgis. You may not have to set it at all; try printing it out first, to see if it is already valid. If setting the correct path does not work, please file a issue ticket.

  3. You may not have to set anything other than the prefixPath, excepting PYTHONPATH, as noted in the Kyngchaos README:

    export PATH="/Applications/QGIS.app/Contents/MacOS/bin:$PATH"

    export PYTHONPATH="/Applications/QGIS.app/Contents/Resources/python"

    If you already have something in PYTHONPATH, add it instead with:

    export PYTHONPATH="/Applications/QGIS.app/Contents/Resources/python:$PYTHONPATH"

    Try not setting DYLD_LIBRARY_PATH, first. If that does not work, try setting only the following:


    Setting DYLD_FRAMEWORK_PATH can help if you have two or more versions of Qt installed on your system. This ensures the PyQt modules imported from the app bundle match with QgsApplication's:


    Append/prepend PATH relative to the command line utilities you may wish to access from your Python script, e.g. via subprocess module.

    OF NOTE: nothing you set in your .bash_profile will be used by the Finder-launched QGIS.app, since GUI apps, by default, do not inherit that environment. You can affect QGIS.app in other ways provided by Apple, or by setting environment variables in the main Options of QGIS >= 2.0 (though not all variables can be successfully set there).

  4. You seem to be using a custom Python install in /usr/local. The Kyngchaos.com QGIS.app is specifically built against Apple's default Mac OS X Python for 10.8 (Python 2.7.x). Different Python installs can only be supported through manual compiling of QGIS source code, unless using something like homebrew or MacPorts. It is best to use the default Python in your standalone script, when leveraging the Kyngchaos.com QGIS.app install Python components.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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