Install 3rd party Python packages into QGIS

Thursday, March 24, 2011

When you write plug-in for QGIS, you may need to rely on additional python packages to work with. However, I find no clue from qgis.org and forum search. After trial and error, here is the approach I used to install 3rd party python packages to QGIS.

Configuration

In my Windows XP, I have 2 copy of Python 2.5.2. One is the original stock version downloaded from www.python.org and installed at c:\python25. Another of course is the one come with QGIS. If you issue
import sys
sys.path

in stock Python console and QGIS Python console respectively, you will get two different results:

Result from stock Python console:
>>> import sys
>>> sys.path
['', 'C:\\windows\\system32\\python25.zip', 'c:\\python25\\DLLs', 'c:\\python25\\lib', 'c:\\python25
\\lib\\plat-win', 'c:\\python25\\lib\\lib-tk', 'c:\\python25', 'c:\\python25\\lib\\site-packages']
Result from QGIS Python Console:
>>> import sys
>>> sys.path
['C:/PROGRA~1/QUANTU~1/apps/qgis/./python', 'C:/Documents and Settings/19/.qgis/python', 'C:/Documents and Settings/19/.qgis/python/plugins', 'C:/PROGRA~1/QUANTU~1/apps/qgis/./python/plugins', 'C:\\PROGRA~1\\QUANTU~1\\apps\\gdal-17\\pymod', 'C:\\Program Files\\Quantum GIS Copiapo', 'C:\\PROGRA~1\\QUANTU~1\\bin\\python25.zip', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\DLLs', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\plat-win', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\lib-tk', 'C:\\PROGRA~1\\QUANTU~1\\apps\\qgis\\bin', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\win32', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\win32\\lib', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\Pythonwin', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\wx-2.8-msw-unicode', 'C:\\PROGRA~1\\QUANTU~1\\apps\\qgis\\python\\plugins\\fTools\\tools']

The above info tell us that Python package install in c:\python25 cannot be shared by Python in QGIS.

easy_install

If you want to install easy_install into QGIS, the setuptool will just tell you that it can only find one copy of Python in c:\python25 in registry:


To let setuptools find QGIS's python copy, go to Registry and look for HKEY_LOCAL_MACHINE\SOFTWARE\Python and export the selected branch to python25.reg beofore any modification. The exported python25.reg will look like this:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Python]

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore]

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5]

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5\Help]

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5\Help\Main Python Documentation]
@="C:\\Python25\\Doc\\Python25.chm"

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5\InstallPath]
@="C:\\Python25\\"

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5\InstallPath\InstallGroup]
@="Python 2.5"

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5\Modules]

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5\PythonPath]
@="C:\\Python25\\Lib;C:\\Python25\\DLLs;C:\\Python25\\Lib\\lib-tk"

In the registry, there is only information of the Python installed in c:\python25 folder. Select HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonPath and change the path to QGIS one. That is:

replace c:\python25 to C:\Program Files\Quantum GIS Copiapo\apps\Python25

After change, export the Python branch again and save it as qgispython.reg for future use.

Also, you also need to add C:\Program Files\Quantum GIS Copiapo\bin and C:\Program Files\Quantum GIS Copiapo\apps\Python25 into PATH. I removed c:\python25 from the PATH when I add the above items into PATH. After installation, I revert the change by adding back c:\python25 and remove the two items.

If you haven't install MinGW, install it and at c:\mingw\bin into PATH. You may also need to tweak the cgwincompiler. Check here for detail.

After the above setups, setuptools should be able to install in QGIS folder:


However, if go to C:\Program Files\Quantum GIS Copiapo\apps\Python25\Scripts and execute easy_install, you will get no responds. The program just return nothing. In this case, you can use easy_install-script.py to install packages. For example, to install Jinja package,

C:\Program Files\Quantum GIS Copiapo\apps\Python25\Scripts>python easy_install-2.5-script.py pyodbc
Traceback (most recent call last):
File "easy_install-2.5-script.py", line 5, in
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources

C:\Program Files\Quantum GIS Copiapo\apps\Python25\Scripts>python eas_install-script.py jinja
python: can't open file 'eas_install-script.py': [Errno 2] No such file or directory

C:\Program Files\Quantum GIS Copiapo\apps\Python25\Scripts>python easy_install-script.py jinja
Searching for jinja
Reading http://pypi.python.org/simple/jinja/
Reading http://wsgiarea.pocoo.org/jinja/
Reading http://jinja.pocoo.org/
Best match: Jinja 1.2
Downloading http://pypi.python.org/packages/2.5/J/Jinja/Jinja-1.2-py2.5-win32.egg#md5=27b0804a126c2c
0ebd4a9dacffb9dcbb
Processing Jinja-1.2-py2.5-win32.egg
creating c:\progra~1\quantu~1\apps\python25\lib\site-packages\Jinja-1.2-py2.5-win32.egg
Extracting Jinja-1.2-py2.5-win32.egg to c:\progra~1\quantu~1\apps\python25\lib\site-packages
Adding Jinja 1.2 to easy-install.pth file

Installed c:\progra~1\quantu~1\apps\python25\lib\site-packages\jinja-1.2-py2.5-win32.egg
Processing dependencies for jinja
Finished processing dependencies for jinja

If you can issue "import jinja" in QGIS Python console without error, it means the installation is done.

Hope the above can help.

p.s. after install the 3rd party package, I will revert the registry (by double click python25.reg saved before )and edit the PATH (add c:\python25 and remove QGIS entries)

Recent search find another approach:
http://forum.qgis.org/viewtopic.php?f=2&t=8201