2

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?

10

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.

  • Thanks for that idea. The bat script seems to run fine, setting everything (as I can see from the echos). Nevertheless, my python script than complains No module named qgis.core. I have to call sys.path.extend([r"C:\Program Files (x86)\QGIS Valmiera\apps\qgis\python"]) explicitly and then end up again with No module named sip. – ulrich May 3 '15 at 12:36
  • You don't have to do sys.path.extend. If you are getting no qgis.core the batch isn't 100% yet. – Nathan W May 3 '15 at 12:42
  • Try the updated script. If it doesn't work, paste what you are using. Did you replace the OSGEO4W_ROOT variable. – Nathan W May 3 '15 at 12:45
  • Yes, I replaced it. Interestingly, now your script runs without complaining from the cmd. If I call it using subprocess.call('path/to/bat-script', shell=True) from my python script, I still get the same no qgis.core. what's the difference here...? – ulrich May 3 '15 at 13:15
  • Why are you calling it like that? – Nathan W May 3 '15 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.