No module named sip when running QGIS from Python

ulrich picture ulrich · May 2, 2015 · Viewed 12.2k times · Source

I have QGIS Valmiera 2.2.0 installed and started a simple python script using this documentation.

I had a hard time because always when I used

from qgis.core import *

I got the message ImportError: No module named qgis.core. I fixed that with the statement

sys.path.extend([r"C:\Program Files (x86)\QGIS Valmiera\apps\qgis\python"])

using the correct python path. Then I expected it to work but I ran into the next error message:

ImportError: No module named sip

I tried to do the same again, adding subfolders of qgis, which might be connected to sip, but without success.

sys.path.extend([r"C:\Program Files (x86)\QGIS Valmiera\apps\Python27",
                 r"C:\Program Files (x86)\QGIS Valmiera\apps\Qt4",
                 r"C:\Program Files (x86)\QGIS Valmiera\apps\Python27\sip",
                 r"C:\Program Files (x86)\QGIS Valmiera\apps\Python27\sip\PyQt4",
                 r"C:\Users\Downloads\sip-4.16.7\sip-4.16.7"])

The sip.exe, which I guess it is looking for, is actually located in the C:\Program Files (x86)\QGIS Valmiera\apps\Python27 path.

Is there anything missing in the pre-installed functionality or what might be the problem?

Thanks for any hints!

UPDATE: For anyone interested: I did the following steps for installing sip library, without final success so far:

Answer

ulrich picture ulrich · May 3, 2015

Got it working now thanks to the answer to my question here.

Actually, it seems like the sip library is really packed with the QGIS distribution but for running a script there it's not only necessary to setup PATH and PYTHONPATH correctly but also to call o4w_env.bat, otherwise it will not be found.

So it is possible to use the bat script from the answer for setting up everything and then calling the python script afterwards which will then be able to use sip and everything.