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)?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
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 ....
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.
Extending @gene's answer:
When using the osgeo4mac homebrew formula
qgis2, there are two options for QGIS python standalone development:
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
You can extend the
PYTHONPATH variable before calling
python inside your terminal:
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
export QGIS_DEBUG = -1
Currently, I also export the following two lines before calling
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.