Friday, 16 October 2015

Where is the python library of QGIS 2.8


where is the Python library for QGIS 2.8 on a mac (10.10)? I cannot find it... sys.path in the QGIS python console gave me some path, but where the interesting part is happening it replaces the folder with dots.


'/Applications/QGIS.app/Contents/MacOS/../Resources/python/plugins/processing' and '/Applications/QGIS.app/Contents/MacOS/../Resources/python


I need to set my python path to use the QGis.core library which requires the Path. I got the instruction from this site http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/intro.html but it seems not be up to date anymore...




Answer



QGIS for Mac (KyngChaos version) is an application bundle ( directory) and the content of QGIS.app is:


enter image description here


The path of the modules is


  /Applications/QGIS.app/Contents/Resources/python 

The path of the qgis library is


  /Application/QGIS.app/Contents/Resources/python/qgis

And of the processing module, for example:



 /Application/QGIS.app//Contents/Resources/python/plugins/processing

If you want to call qgis from outside (in the console, for example), you have two solutions:


1) Define the variables/paths outside Python (Bash)


export PYTHONPATH=/Applications/Qgis.app/Contents/Resources/python
# you can add other modules
export PYTHONPATH=/Applications/Qgis.app/Contents/Resources/python/plugins:$PYTHONPATH
(export PYTHONPAH=/Users/you/.qgis2/python/plugins:$PYTHONPATH if you want)

and



python
.....
from qgis.core import *
QgsApplication.setPrefixPath("/Applications/QGIS.app/Contents/MacOS", True)
QgsApplication.initQgis()
# initialize the app
app = QgsApplication([],True)
layer = QgsVectorLayer('/Users/Shared/test.shp', 'test', 'ogr')
layer.isValid()
True

print QgsApplication.showSettings()
Application state:
QGIS_PREFIX_PATH env var:
Prefix: /Application/QGIS.app/Contents/MacOS
Plugin Path: /Application/QGIS.app/Contents/MacOS/../PlugIns/qgis
Package Data Path: /Application/QGIS.app/MacOS/../Resources
Active Theme Name:
Active Theme Path: :/images/themes//
Default Theme Path: :/images/themes/default/
SVG Search Paths: /Application/QGIS.app/Contents/MacOS/../Resources/svg/

User DB Path: /Application/QGIS.app/Contents/MacOS/../Resources/resources/qgis.db
# import a module
import processing
print processing.__file__
/Application/QGIS.app/Contents/Resources/python/plugins/processing/__init__.pyc

2) Define the paths in Python


python
....
import os

import sys
sys.path.append('/Applications/QGis.app/Contents/Resources/python/')
sys.path.append('/Applications/QGis.app/Contents/Resources/python/plugins')
....

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...