Saturday 21 November 2015

Adding a Python module to QGIS Python in Windows with multiple Python installations

Both Python and (if I rememeber well) QGIS were originally created for Linux. Their use in Windows is somewhat complicated by the difficulties to adapt these tools to a different environment.
When working with an installation of QGIS in Windows created with the standalone installer, one may need to use Python modules that are not present in the default QGIS installation. Modules are generally installed in the folder ..\Python27\Lib\site-packages.
The installation can be confusing if you have also other Python installations, for instance one derived from the Python(x,y) distribution, from Anaconda or from ArcGis.
The QGIS Python installation from the standalone GGIS installer is not system-wide, and QGIS Python is separated and independent from the other Pythons.
After a bit of struggling, I found a way to install a Python module from the source files (tar.gz) in QGIS Python.

Source module

The Python module that I wanted to install is pyserial, that I downloaded as a tar.gz file from https://pypi.python.org/pypi/pyserial.
I uncompressed it in a temp folder, in my particular case:
C:\Temp\dist\pyserial-2.7\pyserial-2.7
This folder contains the required setup.py file, plus some other stuff.

Target QGIS installation 

The target QGIS installation is 2.10.1 Pisa 32bit (from standalone installer), with installation folder:
C:\Program Files (x86)\QGIS Pisa

In QGIS Python, the path to the standard Python module folder is:
..\QGIS Pisa\apps\Python27\Lib\site-packages.
Installing a module in this last directory, we have it recognized by QGIS Python.

Prerequisites

As a prerequisite, you have to be sure to launch from the DOS shell the QGIS Python and not other Python interpreters. To achieve that, one possible (but there are others) way is to add the path to the QGIS python.exe file as the first item in the system PATH.
In Windows 8, follow Control Panel -> System and Security -> System and then , by opening Advanced system settings, modify the Path variable (user or system are equivalent ways, I suppose, in my case I had the system one modified) by adding as the first voice:
C:\Program Files (x86)\QGIS Pisa\bin
i.e., the bin folder of the QGIS installation, where python.exe is contained.

Procedure

Being sure that from the DOS shell you are invoking the QGIS Python, you can then proceed as follows:

1) launch an administrator dos shell, to be sure to have write permissions in the target directory

2) in the dos shell, change directory to the uncompressed folder of the new module to install, (it should contain the setup.py file), in my case with the command:
  cd C:\Temp\dist\pyserial-2.7\pyserial-2.7

3) always in the dos shell, run the python setup.py install command on the module, in my case:
  python setup.py install --prefix="\Program Files (x86)\QGIS Pisa\apps\Python27"
Change path in the prefix option accordingly to your particular QGIS installation. Note two things:
  • do not put the drive character at the beginning of the prefix path; 
  • point to the Python27 folder, not to the  Python27\Lib\site-packages


Checking the results

In addition to the installation messages in the DOS shell, that should present no errors, you may then check that the module installation was successful by trying to import the specific module in the QGIS Python console, in my case:
import serial
You should receive no import error messages..