3

I would like to import qgis.core and utils from my own copy of python. I do have PYQT4 installed there.

I'd be happy to add to my PYTHONPATH or sys.path.append in my code, where do the qgis modules live (on a Mac)?

10

if you use the Kyng Chaos version:

1) First solution with the PYTHONPATH variable:

Add qgis to the PYTHONPATH (terminal)

$ export PYTHONPATH=/Applications/Qgis.app/Contents/Resources/python

You can add this line to your .bash_profile

Then in Python

# Import qgis
from qgis.core import * 
QgsApplication.setPrefixPath("/Applications/QGIS.app/Contents/MacOS", True)
# init the application
QgsApplication.initQgis()

Then you can use PyQGIS

layer = QgsVectorLayer('/Users/Shared/test.shp', 'test', 'ogr')
layer.isValid()
True

2) second solution, all in Python (from Custom Initialization for running standalone PyQGIS processing.):

import os
import sys
sys.path.append('/Applications/QGis.app/Contents/Resources/python/')
sys.path.append('/Applications/QGis.app/Contents/Resources/python/plugins') # if you want to use the processing module, for example
from qgis.core import *
app = QgsApplication([],True)
QgsApplication.setPrefixPath("/Applications/QGIS.app/Contents/Plugins", True)
QgsApplication.initQgis()
import processing
....

If you use the Homebrew version , the PATHs are different (look at Homebrew-osgeo4mac)

| improve this answer | |
  • Thank you for reminding me -- I now recall giving up in the middle of the Kyng Chaos process weeks ago . . . . – J Kelly Nov 24 '14 at 16:35
3

Depends on how you installed QGIS. I downloaded it from qgis.org and this is where its Python packages are:

$ pwd
/Applications/QGIS.app/Contents/Resources/python
$ python -c "import qgis; print qgis.__author__"
Martin Dobias

If that's not the same for you, use the find utility to look for core.so.

| improve this answer | |
  • I swear I tried exactly that but couldn't import core. Oh well, works now! (Also, I didn't know to search for core.SO -- was searching core.PY.) Thanks! – J Kelly Nov 24 '14 at 16:15
  • But . . . . while qgis.core imports, the functions in it show up as NULL. And searching for core.so reveals two such files, neither of them qgis. – J Kelly Nov 24 '14 at 16:35
  • Remember, the QGIS modules are designed to be used within QGIS. Your mileage may vary if you use it otherwise. If your application is geographic but graphical, you could use a) Fiona/Rasterio/Shapely or b) GDAL's Python packages, neither of which depend on Qt or any GUI. – sgillies Nov 24 '14 at 17:56
  • I know. I've been using those a bit, especially GDAL. I wanted it all in python, and some of the GDAL tools seem only to exist in (or, at least, be documented for) bash. Which I can do well enough for myself; I just didn't want to freak others out . . . thought qgis python would be more familiar. – J Kelly Nov 25 '14 at 13:58
1

Extending @gene's answer:

When using the osgeo4mac homebrew formula qgis2, there are two options for QGIS python standalone development:

Option 1

Appending paths inside python:

import os
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
sys.path.append('/usr/local/lib/qt-4/python2.7/site-packages')
from qgis.core import *
app = QgsApplication([],True)
QgsApplication.setPrefixPath("/Applications/QGIS.app/Contents/Plugins", True)
sys.path.append('/usr/local/Cellar/qgis2/2.18.4/QGIS.app/Contents/Resources/python/plugins')
QgsApplication.initQgis()
import processing

It is important that both usr/local/... calls are called before from qgis.core import * and that .../python/plugin is called before import processing.

Option 2

You can extend the PYTHONPATH variable before calling python inside your terminal:

export PYTHONPATH=/usr/local/Cellar//qgis2/2.18.4/QGIS.app/Contents/Resources/python/:/usr/local/lib/qt-4/python2.7/site-packages:/usr/local/lib/python2.7/site-packages:$PYTHONPATH")

This approach is also noted in brew info qgis2:

For standalone Python development, set the following environment variable: export PYTHONPATH=/usr/local/lib/qt-4/python2.7/site-packages:/usr/local/lib/python2.7/site-packages:$PYTHONPATH

I do also recommend setting the QGIS env variable QGIS_DEBUG=1 to suppress debug output messages during your processing calls:

export QGIS_DEBUG = -1

Currently, I also export the following two lines before calling python:

export DYLD_LIBRARY_PATH=/usr/local/Cellar/qgis2/2.18.4/QGIS.app/Contents/MacOS/lib/:/Applications/QGIS.app/Contents/Frameworks/
export QGIS_PREFIX_PATH=/usr/local/Cellar/qgis2/2.18.4/QGIS.app/Contents/MacOS/

However, I'm not sure if this is really necessary. Might delete/update it once I did more testing or received some feedback.

| improve this answer | |

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.