18

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 it's main system Python (C:\Python27), but I need QGIS Python to recognize it.

How can it be done?

21

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.

  • 2
    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 Aug 19 '16 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 ?

  • 1st link doesn't work – nickves Apr 3 '15 at 9:33
  • corrected, thanks and I modified the text for the specific rasterio problem – gene Apr 3 '15 at 12:01
  • The user needed to use this from QGIS. Also, the raster calculator on QGIS was broken, so I was not able to use directly. – George Silva Nov 25 '15 at 19:41
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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