PyQGIS Jupyter notebooks on Windows using Conda
The QGIS conda packages have been around for a while. One of their use cases, for example, is to allow Linux users to easily install multiple versions of QGIS.
Similarly, we’ve seen posts on using PyQGIS in Jupyter notebooks. However, I find the setup with *.bat files rather tricky.
This post presents a way to set up a conda environment with QGIS that is ready to be used in Jupyter notebooks.
The first steps are to create a new environment and install QGIS. I use mamba for the installation step because it is faster than conda but you can use conda as well:
(base) PS C:\Users\anita> conda create -n qgis python=3.9 (base) PS C:\Users\anita> conda activate qgis (qgis) PS C:\Users\anita> mamba install -c conda-forge qgis=3.28.2 (qgis) PS C:\Users\anita> qgis
If we now try to import the qgis module in Python, we get an error:
(qgis) PS C:\Users\anita> python Python 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 08:41:22) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import qgis Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'qgis'
To fix this error, we need to get the paths from the Python console inside QGIS:
import sys sys.path ['H:/miniconda3/envs/qgis/Library/./python', 'C:/Users/anita/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python', ... ]
This list of paths can be configured as the defaults for our qgis environment using conda develop:
(qgis) PS C:\Users\anita> conda activate base (base) PS C:\Users\anita> mamba install conda-build -c conda-forge (base) PS C:\Users\anita> conda develop -n qgis [list of paths from qgis python console]
With this setup, the import should now work without errors:
(base) PS C:\Users\anita> conda activate qgis (qgis) PS C:\Users\anita> python Python 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 08:41:22) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import qgis
The example Jupyter notebook covers running a QGIS Processing algorithm and visualizing the results in the notebook using GeoPandas:
Head over to Github to find the full instructions: https://github.com/anitagraser/QGIS-resources/blob/master/qgis3/notebooks/hello-world.ipynb
Thank you for the very interesting and useful post.
Unfortunately, I encounter few errors:
1. mamba fails with error (from libmambapy.bindings import * # noqa: F401,F403
ImportError: DLL load failed while importing bindings) but ok, workaround is to use conda although less efficient and got stuck several times with frozen solve
2. when launching qgis from command prompt I receive the following error: “Missing module ScriptRunner needs the win32api module, but it is not installed.
You can install it from the command line using: python3 -m pip install pywin32
See http://loc8.cc/ppg3/quickstart for help in setting up your command line environment.”
Qgis still loads with critical (red) warning: “Authentication System: DISABLED. Resources authenticating via the system can not be accessed”. If I install pywin32 as suggested it breaks the environment and qgis doesn’t start anymore….
I am on windows 10 (win64), Miniconda (conda 23.1.0)
Dear Dorakiara, unfortunately it is hard to debug these issues in the comments section. Please post your question to e.g. gis.stackexchange.com, the QGIS Telegram group or Matrix channel (https://qgis.org/en/site/forusers/support.html#support). I would avoid pip installing packages if not absolutely necessary. I assume that pywin32 was installed in the wrong environment. Try conda installing pywin32 in the qgis environment.
First of all, I would like to thank you for this guide, as it is the first that I could understand. I was wondering if you could help me? I followed the guide but didn’t get any errors when trying to import qgis the first time. I finished all the steps and vscode still doesn’t recognize any of qgis imports. Thanks again!
Dear Ivan, unfortunately it is hard to debug these issues in the comments section. Please post your question to e.g. gis.stackexchange.com, the QGIS Telegram group or matrix channel (https://qgis.org/en/site/forusers/support.html#support) and include details about your operating system and your VSCode configuration. Did you pick the qgis conda environment in VSCode? Did you get an error?
I did pick and activate the qgis conda environment, but didn’t get the error: ModuleNotFoundError: No module named ‘qgis’ at all. QGis starts fine when initialized from the terminal, but the vscode doesn’t recognize any of the imports from qgis.
Please share a Screenshot of vscode that shows the activated qgis conda environment and the error message
First of all, I would like to thank you and apologize for taking time from your busy schedule. Here is a screenshot after qgis installation: https://ibb.co/V34fvc7. Qgis starts when the command qgis is entered into the terminal, and there’s no error after the command import qgis.
To debug the issue in vscode, it is necessary to see the settings in vscode and the error there