QGis python bindings with arcpy
I have very mature development environment in python 2.7.5, centered around ArcPy (ArcGIS python bindings) and iPython (a very useful python shell). I just updated my QGis install (using OSGeo4W64), and I thought I would try to add the QGis python bindings to my existing install - here is how to do it!
ArcGIS (10.2) comes with a Python 2.7.5 install. I have also installed the 64-bit python geoprocessing for ArcGIS, and I have that fully setup. For interactive testing, geoprocessing and running scripts I have found iPython very useful. This is a very useful console/shell for python, with tab completion and various useful tricks. I run this in a tab in the ConEmu console emulator. This is all on Windows 7 64-bit, by the way.
For QGis, I have used the very handy OSGeo4W distribution (64-bit flavor), which wraps a number of free and open source GIS and geography related applications.
Now, OSGeo4W comes with a python distribution that is ready and configured for QGis (and other assorted tools). But I find that two python (ArcGIS 32-bit and 64-bit) installs are enough to keep track of, and I am not keen on adding iPython again to the OSGeo4W mix.
To add the qgis python bindings to the existing python installs were not overly straight-forwards, and I bumped into a lot of errors on the way.
Here are the steps that worked for me:
- Make sure that OSGeo4W and python is installed properly
- In the OSGeo4W python, find out what version of PyQt4 is installed using these steps:
- Start the OSGeo4W Shell
- type python
- after the interpreter/shell has loaded, type:
from PyQT4 import QtCore
QtCore.PYQT_VERSION_STR - For me, this printed '4.10.2'
- Now you need to download and install the matching PyQt4 library. For my 64-bit python 2.7.5 with QGis 2.2.0 this meant this file.
- Now you need to set your environment to point at the correct folders. If you have installed OSGeo4W elsewhere, adjust the values accordingly:
- Add C:\OSGeo4W64\apps\qgis\python to your PYTHONPATH environment variable. You can separate multiple paths by ;.
- When executing python, add the following paths to your PATH environment variable. I added this to a batch-file that executes iPython, since I prefer to not pollute my system-wide path too much:
set path=C:\OSGeo4W64\apps\qgis\bin;
C:\OSGeo4W64\apps\qgis\python;C:\OSGeo4W64\bin;%path%
- When you run python now (or iPython) try:
import PyQt4.QtGui
import PyQt4.QtCore
from qgis.gui import *
from qgis.core import *
For me, that imports without errors now!