Friday, 8 January 2016

Can we avoid using SIP library when writing standalone QGIS python script



I wanted to write a small custom python script using QGIS functionality. So I started with the QGIS documentation on the custom application section.


After having some troubles with the import qgis.core statement, which I could fix, I ran into the error message


ImportError: No module named sip

This one made me somewhat of a headache, as you could see here.


As I could not fix this one, I wonder why it's so much of a problem. Also, the doc states



"PyQGIS bindings depend on SIP and PyQt4."



So I wonder, why it actually complains. Shouldn't it then come with the QGIS distribution? (Actually, there is a sip folder containing PyQT4 folder and also a sip.exe. But this does not seem to be of any effect.)



After all, my question is, is it possible to work around it, getting QGIS python running without SIP?



Answer



No you can't. The Python bindings need sip so you will have to get it working. However it is included with QGIS so if your getting that error something isn't setup right.


When ever I create a standalone QGIS Python application (on Window at least) I use the following batch file:


REM Change OSGeo4W_ROOT to point to your install of QGIS.

SET OSGEO4W_ROOT=C:\OSGeo4W
SET QGISNAME=qgis
SET QGIS=%OSGEO4W_ROOT%\apps\%QGISNAME%
set QGIS_PREFIX_PATH=%QGIS%


CALL "%OSGEO4W_ROOT%\bin\o4w_env.bat"

: Python Setup
set PATH=%OSGEO4W_ROOT%\bin;%QGIS%\bin;%PATH%
SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python27
set PYTHONPATH=%QGIS%\python;%PYTHONPATH%

ECHO OSGeo path is: %OSGEO4W_ROOT%
ECHO Getting QGIS libs from: %QGIS%

ECHO Python loaded from: %PYTHONHOME%

python myscript.py

You need to change OSGEO4W_ROOT to point to the base of your QGIS install. This is just the top level folder e.g C:\Program Files (x86)\QGIS Valmiera everything else in that script is relative to that root folder.


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