TransWikia.com

Installing 3rd party python libraries for QGIS on Windows

Geographic Information Systems Asked by George Silva on March 3, 2021

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.

6 Answers

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:OSGeo4W64appsPython27 and the modules accordingly at C:OSGeo4W64appsPython27Libsite-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.

Correct answer by Kersten on March 3, 2021

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 ?

Answered by gene on March 3, 2021

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.

Answered by Pietro Tou on March 3, 2021

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')

Answered by greenwd on March 3, 2021

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.

Answered by amit haldar on March 3, 2021

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.

Answered by nospec on March 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP