26

How can I use 3rd party libraries on QGIS plugins on Windows?

I've developed a plugin that uses rasterio and numpy for a customer, but he's having problems installing rasterio and numpy.

Actually, rasterio and numpy were installed in its main system Python (C:\Python27), but I need QGIS Python to recognize it.

0

6 Answers 6

26

QGIS, as distributed by OSGeo4W, usually comes with its own Python installation and its own packages that are independent of your "regular" Python installation.

The easiest way to install a Python package into the OSGeo4W distribution is to open the OSGeo4W Shell and use pip from there. This will install the package into the Python distribution QGIS uses, in my case located at C:\OSGeo4W64\apps\Python27\ and the modules accordingly at C:\OSGeo4W64\apps\Python27\Lib\site-packages. You can also do a regular pip list inside the OSGeo4W Shell and your regular Windows Shell (cmd.exe) and compare the outputs to see what packages you might be missing.

If you don't want to install packages to two Python installations you could also try to change the PythonPath to include packages from one installation into the other.

edit: This answer is directed at the original question regarding pip to install modules to be used with QGIS in Windows. OP has since edited/refined the question so this answer is a bit broad now.

1
  • 5
    To use pip in OSGeoW shell I had to do python -m pip install wheel setuptools c:\path\to\rasterio-<ver>-win_amd64.whl
    – chip
    Commented Aug 19, 2016 at 16:06
13

Numpy is installed by default in QGIS (Standalone version, OSGeo4W version). For rasterio, there are many problems. I know that rasterio is powerful and easy to use (I use it) but why rasterio in a QGIS plugin while you have all the functions of PyQGIS ? (it was designed for the Python shell or for scripts, not to be used with QGIS/PyQGIS, as Fiona for the shapefiles)

In Linux or Mac OS X (Unix systems), QGIS uses the standard Python installation (independent of QGIS) and if is easy to install or upgrade a Python module (there are also many compilers installed by default)

In Windows, Python is not installed by default, and QGIS installs his own version. To install pure Python modules (without C, C++ libraries) you have solutions (QGIS Standalone and the Python Modules, Installing Python setuptools into OSGeo4W Python or Installing PySAL for OSGeo4W)

But for modules that needs to be compiled (no compiler by default), it is not easy. You can try to copy/paste your installed module in the QGIS Python site_folder but there are problems.

Rasterio need to be compiled against an installed GDAL library and if you install it from Christoph Gohlke Unofficial Windows Binaries for Python Extension Packages, for example, I am not sure that the version of GDAL used by QGIS is the same (for the references to the dll libraries)

If you use exclusively numpy and rasterio, why not an independent script ?

3
  • The user needed to use this from QGIS. Also, the raster calculator on QGIS was broken, so I was not able to use directly. Commented Nov 25, 2015 at 19:41
  • @gene are you aware of any changes since 2015 so that users can incorporate packages such as rasterio or GeoPandas into QGIS plugins?
    – Aaron
    Commented Jan 22, 2021 at 18:05
  • 1
    Sorry, but for Windows no (I don't use Windows)
    – gene
    Commented Jan 22, 2021 at 21:01
12

The simple way I found to install libraries for Python in QGIS is

  1. Open OSGeo4W Shell as an administrator

  2. Type py3_env in the console (This should print paths of your QGIS Python installation)

  3. Type python -m pip install {your library}

and its done.

6

From within the Qgis 2.18.28 's python 2.7.14 console, this works for me. It avoids having to use prompts with absolute python paths.

import subprocess
subprocess.check_call(['python', '-m', 'pip', 'install', 'openpyxl'])

According to pip developers, pip.main or pip._internal.main are not supported. https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program I modified the recommended sys.executable to 'python', because in Qgis it is qgis itself.

2
  • 1
    This does not work for me in QGIS 3.24
    – G M
    Commented Apr 13, 2022 at 15:16
  • Works now for me in QGIS 3.34.9
    – ThiPa
    Commented Aug 29 at 7:56
5

You can put 3rd party lib into your plugin folder and then add it to the path with the following script in plugin_name\__init__.py:

import sys, os
sys.path.insert(0, str(os.path.join(os.path.dirname(os.path.realpath(__file__)), ''))+'\lib_dir')
1
  • Does this create lock on lib_dir folder? I tried similar solution using site: "import site ; site.addsitedir(lib_dir)". It works perfectly only problem is i cant uninstall plugin cause of folder lock of the mofulr in lib_dir, in my case lxml module.
    – Mat
    Commented Mar 5, 2020 at 10:25
1

The most efficient way to do this is with conda. This will work for both Windows and MacOS.

First install Miniconda/Anaconda if you do not already have conda installed in your system.

Then create a new environment, by typing the following in Anaconda Prompt for Windows machines or terminal in Mac:

$conda create -n <your_env>

then install QGIS with:

$conda install -c conda-forge qgis

Then to run QGIS:

$conda activate <your_env>
$qgis

To install third party packages, first quit QGIS and type the following in the activated env:

$conda install -c conda-forge rasterio 
$conda install -c anaconda numpy

Remember to activate your environment before installing your packages. after activation pip install should also work.

2
  • Have you tried it?
    – nospec
    Commented Feb 2 at 17:48
  • 2
    No idea how but somehow I missed the "then install QGIS" line. Sorry! This is a good approach albeit kinda technical for most users unless coupled with a launcher file. Commented Feb 4 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.