[TUTORIAL] Create an anaconda environment for PyQGIS development

In this tutorial, we will learn how to install anaconda, create and configure an anaconda environment for PyQGIS development including standalone development.

What is anaconda and PyQGIS ?

Anaconda is the most popular data science platform in the world. It uses conda which is a management software to create environment for development in any languages. Anaconda helps to manage dependencies between packages and libraries.
PyQGIS is the Python API for QGIS development which permits to use all the QGIS functions (including plugins) in Python Scripts.

Installation of Anaconda


Go to https://www.anaconda.com/distribution/#linux and download the Anaconda installer for Linux. Open a terminal (CTRL+ALT+T) and run the following command :
bash ~/Downloads/Anaconda3-2019.10-Linux-x86_64.sh
Then follow the installation instruction.
If you don’t want the base environment to be activate by default when you open a terminal you can :
conda config --set auto_activate_base False


Go to https://www.anaconda.com/distribution/#download-section and choose the Graphical Installer corresponding to your machine. It’s a classic .exe file, nothing difficult.

Creating an environment for PyQGIS development

For Linux users, open a terminal, if you disable the base activation you can :
conda activate
You should have after this a (base) prefix at the left of your machine name.
For Windows users, search for the anaconda prompt and open it.
To create the environment, you first need to know which version of Python your QGIS installaton use. To do this, you can open QGIS, load the Python Console (CTRL+ALT+P) and run the following command :
import sys

You should have an output like this :
3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0]

You need now to create the conda environment with the matching python version, for my part an environment call “conda-qgis” with the 3.6.9 Python version. In the terminal or the anaconda prompt :
conda create conda create -n conda-qgis python=3.6.9
You now have your environment with the good python version. We now need to set up the environment with the QGIS Python API (PyQGIS) and the Python Libraries that QGIS use (GDAL, GRASS, SAGA…). Just for the information, anaconda allow to install QGIS in an environment like a package (https://anaconda.org/conda-forge/qgis), but for some reasons you can’t import QGIS modules with this installation. We are going to set up the paths manually but know that in the future this package might be functional.
To set up the paths for your new environment, you need to activate conda on the base environment (if you have the (base) prefix you’re good, otherwise pass the command conda activate).
To get the paths that QGIS use, go to the python console in QGIS and pass the following commands :
print(‘ ‘.join(sys.path))
You might have an output like this :
/usr/share/qgis/python /home/etienne/.local/share/QGIS/QGIS3/profiles/default/python /home/etienne/.local/share/QGIS/QGIS3/profiles/default/python/plugins /usr/share/qgis/python/plugins /usr/lib/python36.zip /usr/lib/python3.6 /usr/lib/python3.6/lib-dynload /home/etienne/.local/lib/python3.6/site-packages /usr/local/lib/python3.6/dist-packages /usr/lib/python3/dist-packages /home/etienne/.local/share/QGIS/QGIS3/profiles/default/python /usr/lib/python3/dist-packages/IPython/extensions /home/etienne/.local/share/QGIS/QGIS3/profiles/default/python/plugins/mmqgis/forms /home/etienne/Documents/Atlas
For windows users the path are different, but the principe is the same. Now that we have the paths, let set up the environment that we created for PyQGIS development. Copy the QGIS Python console output and go to anaconda Prompt or Linux terminal with base environment activated, then pass this command :
conda-develop [PATHS] -n conda-qgis
Where ‘[PATHS]’ is the paths that you just copy from QGIS. Once this command is done, you might have in the folder of your environment a ‘conda.pth’ file which is a simple text file for anaconda to search external paths.

PyQGIS Standalone

If your point is to use QGIS algorithms in standalone Python SCRIPT (without the QGIS interface), there’s an extra thing to do. We need to find the wrappers.py file for QGIS gui processing, on Linux you can :
sudo find -iname wrappers.py
and search in the results the wrappers.py file for QGIS.
In Windows you can go to anaconda prompt and pass the following commands :
conda activate conda-qgis
>>> import qgis.core
>>> import processing
>>> processing.algorithmHelp(‘gdal:warpreproject’)

You might have an error message with the path for wrappers,py.
Go to the wrappers.py directory and make a copy of this file on your desktop for example, then open it and make a CTRL+F to search for \A. Once you identify the two lines where those \A are, just place a r before the ‘. Those line should now looks like :
match = re.search(r'(?:\A|[^0-9]){}(?:\Z|[^0-9]|)'.format(v), opt)
match = re.search(r'(?:\A|[^0-9])([0-9]+)(?:\Z|[^0-9]|)',
You can now save the changes and place the new wrappers.py in the original folder (replace the old one).

You’re now good for using your environment to write scripts, plugins or even applications using the QGIS Python API in QGIS or standalone.