[GIS] How to Setup Python to recognize QGIS

importerrorpyqgispython-2.7

I want to use QGIS and work with Python out of the console, but I'm having troubles letting the system recognize the paths of some DLLs.
Since I couldn't find a good answer on the web, I wonder if I did something wrong.
I'm working with Windows 7 64 bit version, using Python 2.7 and QGIS 1.8.
In the system environment, I've created and pointed the variable PYTHONPATH to the python directory (which in my case is "C:\Quantum_GIS_Lisboa\apps\qgis\python"). Then I've modified the "Path" variable adding the address where "qgis_core" and "qgis_gui" are found (in my case "C:\Quantum_GIS_Lisboa\apps\qgis\bin"). The instructions I've followed are those of the "PyQGIS Developer Cookbook" ("http://www.qgis.org/pyqgis-cookbook").
It seems the problem relies in recognizing the DLLs, which are in the folder I've pointed to for the variable "Path".
Here is the error I have in the Shell when I try to import the module "qgis.core":

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
     import qgis.core
ImportError: DLL load failed: Impossibile trovare il modulo specificato.

Moreover, if I import the sys module and print the path to see what's there, I'm not finding what I expect:

>>> import sys
>>> print sys.path
['C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\ArcGIS\\Python 2.7', 'C:\\Python27\\ArcGIS10.1\\Lib\\idlelib', 'C:\\Quantum_GIS_Lisboa\\apps\\qgis\\python', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\ArcGIS10.1\\DLLs', 'C:\\Python27\\ArcGIS10.1\\lib', 'C:\\Python27\\ArcGIS10.1\\lib\\plat-win', 'C:\\Python27\\ArcGIS10.1\\lib\\lib-tk', 'C:\\Python27\\ArcGIS10.1', 'C:\\Python27\\ArcGIS10.1\\lib\\site-packages', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.1\\bin', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.1\\arcpy', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.1\\ArcToolbox\\Scripts', 'C:\\Python27\\ArcGIS10.1\\lib\\site-packages\\win32', 'C:\\Python27\\ArcGIS10.1\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\ArcGIS10.1\\lib\\site-packages\\Pythonwin']

Hope someone could help me solving this, I'd be really thankful!

I've just tried to copy and paste the whole content of Python27 folder within the main "Quantum GIS Lisboa" folder in the Python27 main folder in my hard drive C, since I saw ArcGIS and Python were working fine like that. Then I tried to launch the IDLE found in "lib/idlelib" ("idle.pyw") from the new folder but this didn't solve the problem.
Everything seems to point to the right folders in the system environment.
Here is the print of the sys.path after the "reverse" trick:

['C:\\Quantum_GIS_Lisboa\\apps\\qgis\\bin', 'C:\\Quantum_GIS_Lisboa\\apps\\qgis\\python', 'C:\\Python27\\ArcGIS10.1\\lib\\site-packages\\Pythonwin', 'C:\\Python27\\ArcGIS10.1\\lib\\site-packages\\win32\\lib',
'C:\\Python27\\ArcGIS10.1\\lib\\site-packages\\win32', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.1\\ArcToolbox\\Scripts', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.1\\arcpy', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.1\\bin', 'C:\\Python27\\ArcGIS10.1\\lib\\site-packages', 'C:\\Python27\\ArcGIS10.1', 
'C:\\Python27\\ArcGIS10.1\\lib\\lib-tk', 'C:\\Python27\\ArcGIS10.1\\lib\\plat-win', 'C:\\Python27\\ArcGIS10.1\\lib', 'C:\\Python27\\ArcGIS10.1\\DLLs', 'C:\\Windows\\system32\\python27.zip', 'C:\\Quantum_GIS_Lisboa\\apps\\qgis\\bin', 'C:\\Quantum_GIS_Lisboa\\apps\\qgis\\python', 'C:\\Python27\\ArcGIS10.1\\Lib\\idlelib', 'C:\\Users\\Umberto\\Desktop']

Traceback (most recent call last):
  File "C:\Users\Umberto\Desktop\set_qgis_01.py", line 5, in <module>
    import qgis.core
ImportError: DLL load failed: Impossibile trovare il modulo specificato.

Am I suppose to point to the Quantum GIS main folder as well as it contains a folder named "lib"?

Best Answer

It looks like you have a similar problem to one I had. Though the specifics of the paths are different I believe that a similar solution can work. See my solution in this post.

What is happening is that it is not just the PYTHONPATH which is read but also the PATH variable. On load QGIS appends the paths of the osgeo binaries to the path, which means they get read last. That means the ArcGIS location is found first and Windows looks no further.

What you don't want to do is muck up your ArcGIS install by playing with the main PATH variable (where there will be a reference to the Arc Python directory). However, if you tell QGIS to reverse the sys.path variable as the first thing it does, it will read the osgeo paths first and ignore the Arc Python instalation. This change is only apparent within the specific Python session (i.e. it has no effect on the actual PATH or PYTHONPATH variables stored on your computer and neither is it permanent, so will be safe as far as ArcGIS is concerned and less dangerous than editting your PATH variable as this could end up breaking Arc with the same problem but the other way around).

Depending on your install, you might want to change my solution to something like (based on what I read in your question):

import sys
sys.path.extend([r"C:\Quantum_GIS_Lisboa\apps\qgis\python",r"C:\Quantum_GIS_Lisboa\apps\qgis\bin"])
sys.path.reverse()

If you have used osgeo4w to install QGIS this will be unnecessary, but you could do it anyway as a belt-and-braces approach - it won't hurt). I suspect you will also need to add the lines at the start of the set_qgis_01.py file (I did mine in the GDALTOOLS.py file because that's where my conflict manifested itself).

Related Question