Remote Sensing tips and tricks

Using PyQGIS in Mac OS X

The QGIS built in Python console is great. But the real power comes in being able to use PyQGIS in external scripts.

Without completing the following steps, you will encounter this situation when trying to import the modules:

>>> import qgis.core
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ImportError: No module named qgis.core

The PyQGIS developer cookbook explains how to fix this problem for Windows and Linux, but not Mac. So Mac users, here’s what you do.

Open your ~/.profile for editing. Add the following lines:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/Applications/

    export PYTHONPATH=$PYTHONPATH:/Applications/

    export PATH=$PATH:/Library/Frameworks/GDAL.framework/Programs/

Add this line to load external opengeo libraries, so you can save out your layers in various formats.

export PYTHONPATH=$PYTHONPATH:~/.qgis2/python/plugins/opengeo/ext-libs/

Save this and type the following into the terminal.

source ~/.profile

Now you will be able to import PyQGIS modules into any of your external scripts.

Spot an error raise a ticket