Installing Third-party Python Modules in QGIS (Windows)

MARCH 02, 2016 pyqgis python qgis sysadmin

We’ve recently been developing a client plugin for forest management which relies on the Python SQLAlchemy module. When I installed the latest version of my colleague’s work I got a Python traceback error about a missing Python module (SQLAlchemy).

This blog is more of a note to myself for future reference of how to install third-party modules in QGIS’ Python environment.

The Problem

Installing third-party modules in Windows is usually quite straight-forward, either download an installer (which will find the Python environment from the registry) or use easy_install from the Python setuptools.

The problem is that QGIS ships with its own Python installation that these methods cannot easily add to.

(Don’t get me wrong - I like the fact that each QGIS instance has its own Python environment as it keeps each version very self-contained).

The Method

So here’s the method I successfully used to install SQLAlchemy (which I’ll use here to install the lxml module):

  • Click Start
  • Type QGIS
  • Wait for the QGIS version / instance you want to modify to appear:

  • Right-click it, select Run as administrator
  • In QGIS, select Python Console from the Plugins menu
  • Download from here.
  • Run the following code on the python console in QGIS:
from subprocess import call
# Replace the path below to the location of that
# you just downloaded
call(['python', r'C:\Users\Pete\Downloads\'])

# This will install *setuptools* which is a package manager
# The previous command should return 0 on success

# Replace lxml with the package you wish to install
call(['easy_install', 'lxml'])

# Again this will return 0 on success
  • Restart QGIS (normally, not using the Run as administrator option)

The new module should now be available, we can test this (again on the Python console in QGIS):

import lxml

That’s it!

You may also like...

Mergin Maps, a field data collection app based on QGIS. Mergin Maps makes field work easy with its simple interface and cloud-based sync. Available on Android, iOS and Windows. Screenshots of the Mergin Maps mobile app for Field Data Collection
Get it on Google Play Get it on Apple store

Posted by Peter Wells