Configure PyScripter to use with QGIS (and still use arcpy) on Windows

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:

import qgis.core

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. Also

.csharpcode {
background-color: #ffffff; font-family: consolas, “Courier New”, courier, monospace; color: black; font-size: small
}
.csharpcode pre {
background-color: #ffffff; font-family: consolas, “Courier New”, courier, monospace; color: black; font-size: small
}
.csharpcode pre {
margin: 0em
}
.csharpcode .rem {
color: #008000
}
.csharpcode .kwrd {
color: #0000ff
}
.csharpcode .str {
color: #006080
}
.csharpcode .op {
color: #0000c0
}
.csharpcode .preproc {
color: #cc6633
}
.csharpcode .asp {
background-color: #ffff00
}
.csharpcode .html {
color: #800000
}
.csharpcode .attr {
color: #ff0000
}
.csharpcode .alt {
background-color: #f4f4f4; margin: 0em; width: 100%
}
.csharpcode .lnum {
color: #606060
}

set OSGEO4W_ROOT=C:\PROGRA~2\QUANTU~1
call%OSGEO4W_ROOT%“\bin\o4w_env.bat
call%OSGEO4W_ROOT%“\apps\grass\grass-6.4.2\etc\env.bat
set GDAL_DRIVER_PATH=%OSGEO4W_ROOT%\bin\gdalplugins\1.9
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass-6.4.2\lib
path %PATH%;”%OSGEO4W_ROOT%\apps\Python27\Scripts\”
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python;
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\Lib\site-packages
set QGISPATH=%OSGEO4W_ROOT%\apps\qgis
start “PyScripter” /B “C:\Program Files (x86)\PyScripter\PyScripter.exe” –python27

Happy coding!

, , ,

  1. #1 by Don on July 16, 2012 - 22:26

    Just want to say thanks for taking time to teach through this post. I’m sure it will come in handy when I branch out soon from ArcGIS and arcpy to QGIS.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 123 other followers

%d bloggers like this: