For a long time I wanted to give a go at creating a QGIS plugin. Nothing in particular, just a ‘Hello World’ plugin or similar so I could understand how the whole QGIS/python thing hangs together. Seasoned readers of my blog may have noticed by now that I mainly (only?) work on Windows OS. And let me tell you, trying to find exactly what is needed in terms of python libraries for being able to create a simple form in QGIS proved to be a big challenge. .
But I have figured it out and those are my notes in case you also want to give it a try. They don’t go as far as creating a plugin. They merely create a custom form you can use for editing a layer. But I consider this a HUGE step for me nevertheless!
I was running QGIS 1.8 on a Windows 7 64-bit laptop. I also had ArcGIS 10 installed and for Python development I use PyScripter. This posed an interesting problem of how to use PyScripter for both ArcGIS and QGIS but I had solved this earlier
So next, I apparently had to use PyQt4 for being able to create forms within QGIS. But DO NOT use the version for Python 2.7 from this link as -as I found out at my expense- its not compatible with QGIS. The safest way is to use the OSGeo4W Installer. Select the Advanced install and select to download and install the packages as shown in the next screenshot. Not entirely sure that ALL these are required but they work for me so….
Note the last item on the list (SIP) which the version I have is the previous from the current one. I *think* you need this version otherwise you get runtime errors when trying to load the PyQt4 libraries i.e. “RuntimeError: the sip module implements API vXX to vXX but the PyQt4.QtCore module requires API vXX”
Yes- it was a very trial and error job…
Moving on, you can then open PyScripter for Python 2.7 and check if you can load the PyQt4 libraries by running on the python prompt:
>>> from PyQt4.QtCore import *
>>> from PyQt4.QtGui import *
If all goes well you should not get any errors. To be on the safe side and ensure you have the same setup in the QGIS python environment (it may sound an unecessary step but trust me, its not) run the above command on the QGIS python window as well.
And so, finally, you are ready to make your custom form. But for this I simply followed Nathan’s example from his QGIS blog
Next step would be to create my Hello World plugin. On a next post….
As many of you may already know, PyScripter is an excellent IDE for Python. For those of you who haven’t tried it… well, you don’t know what you are missing! I was using PyScripter quite happily with ArcGIS 10 and arcpy without ever having to tweak anything in PyScripter. All was well until I decided to explore PyQGIS, QGIS’ scripting language. I could not, for the life of me, get this initial statement to work in PyScripter although it was working fine in the QGIS Python console:
I was getting the “usual” error:
Traceback (most recent call last):
File “<interactive input>”, line 1, in <module>
ImportError: No module named qgis.core
Now, arcpy is using Python 2.6 whereas QGIS (I was using the latest 1.8 Lisboa version) is using 2.7. One would have thought that since PyScripter allows you to work with different versions of Python this wouldn’t be such a big deal. Yet it was. Below I am outlining the steps I took to solve it.
One thing to note here is that I installed QGIS 1.8 using the standalone windows installer. Things may have been different if I used the OSeo4W installer which apparently sets the environment for you….but I can’t be sure.
One of the problems with the standalone installer is that it installs Python 2.7 but does not create any registry entries- which is what PyScripter mainly uses. So the first thing to do is to create the relevant registry entries for 2.7. Assuming you have installed QGIS in the default folder (C:\Program Files (x86)\Quantum GIS Lisboa for a 64bit machine), you can use this registry settings file (edit the file accordingly if using 32-bit Windows).
Next, copy the python27.dll from C:\Program Files (x86)\Quantum GIS Lisboa\bin to c:\Windows\SysWOW64 (or \system32 for 32-bit machines)
Finally, create this batch file to start pyscripter with (note you may change the PROGRA~2 to PROGRA~1 for 32-bit machines.
start “PyScripter” /B “C:\Program Files (x86)\PyScripter\PyScripter.exe” –python27