Standalone applications using QGIS and environment variables
Consulting the PyQGis documentation, you’ll find that there are four main ways to utilize the PyQGis API:
- via commands in the Python console
- via Python scripts in Processing or with the ScritRunner plugin of Gary Sherman
- via development of custom plugins to the QGis application
- outside QGIS (in the Python shell or creating an applications with PyQt4 and not Tkinter (why import Tkinter twice ?)
and you are interested in this last point:
- you can use PyQGIS as any other Python module. But Python does not know where to find PyQGIS. For that, you need to add the PyQGIS folder to the PYTHONPATH (for Windows, look at How to add to the pythonpath in windows 7?).
Certainly ArcPy doesn't require people to mess around with the computer's environmental settings, so I'm having difficulty understanding why PyQGIS does
Because you use the Python version of ArcGIS, in other cases, the same is true, look using arcpy outside arcmap or Configure PyScripter to use with QGIS (and still use arcpy) on Windows, for example.
You don't need here PyQt4, Tkinter or qgis.gui:
from qgis.core import *
QgsApplication.setPrefixPath("yourpath", True)
QgsApplication.initQgis()
# or your solution
# read a shapefile
layer = QgsVectorLayer('your.shp', 'your', 'ogr')
layer.isValid()
True
# loop through layer
for elem in layer.getFeatures():
geom= elem.geometry()
attr =elem.attributes()
(processing)
# interaction with other Python module: Shapely, for example
from shapely.geometry import shape
from json import loads
for elem in layer.getFeatures():
shapely_geometry = shape(loads(elem.geometry().exportToGeoJSON()))
- you can create an application. You need here PyQt4 (and not Tkinter) and qgis.gui.
I would like to program a standalone application using PyQGIS the way I can already program standalone applications using ArcPy.
So, for that, you must learn PyQt4 , as you have to learn Tkinter (or wxPython), for example. This is another problem: the solution given by gsherman is a problem of PyQt4, not of PyQGIS (look at PyQt4 tutorial, for example)
You have to start the Qt event loop using:
app.exec_()
I would remove QgsApplication.exitQgis()
. It should ideally be handled in a slot that is connected to lastWindowClosed()
signal of the application.