4

System: Windows 10

conda version : 23.3.1

I have setup a conda environment with qgis according to this: https://gisunchained.wordpress.com/2019/05/29/using-qgis-from-conda/

However, I used the newest version of python (which is now in 2023 python 3.11) and the stable qgis version 3.28.5.

So:

C:\Users\iik> conda create –name with_qgis python=3.11
C:\Users\iik> conda activate with_qgis
(with_qgis) C:\Users\iik> conda install qgis=3.28.5 --channel conda-forge

Then I added the kernel to jupyter notebook

(with_qgis) C:\Users\iik> conda install ipykernel
(with_qgis) C:\Users\iik> python -m ipykernel install --user --name with_qgis

Generally I'm then able to open jupyter notebook, select the relevant kernel and use the installed libraries. But importing qgis like this in jupyter:

import qgis

Just gives the error:

ModuleNotFoundError: No module named 'qgis'

IMPORTANT!!! If I activate the conda environment in the terminal, activate python and then import qgis, I get no error at all, but all seems to work fine.

(with_qgis) C:\Users\iik> python
>>>import qgis
>>>

So I assume, the library is correctly installed, but jupyter notebook cannot find the library?

What do I need to do to be able to import qgis into the jupyter notebook?

3
  • 1
    Activate the conda environment in the terminal, then start jupyter from that terminal.
    – user2856
    Apr 13, 2023 at 23:46
  • For this approach I would need to install jupyter into that conda environment. Is that what you mean?
    – i.i.k.
    Apr 14, 2023 at 6:57
  • I think I found an interesting discussion on this import error, when using jupyter notebooks and conda environments, which may help to understand the problem: stackoverflow.com/questions/58068818/…
    – i.i.k.
    Apr 14, 2023 at 7:07

1 Answer 1

4

The solution to use qgis in a conda environment within a jupyter notebook is to install qgis as well as jupyter notebook into the conda environment, and then activate jupyter notebook from inside the conda environment, so:

C:\Users\iik> conda create –name with_qgis_jptr python=3.11
C:\Users\iik> conda activate with_qgis_jptr
(with_qgis_jptr) C:\Users\iik> conda install jupyter
(with_qgis_jptr) C:\Users\iik> conda install qgis=3.28.5 --channel conda-forge
(with_qgis_jptr) C:\Users\iik> jupyter notebook

This opens the jupyter notebook browser, where you then create a new jupyter notebook using the 'ipykernel' in the jupyter menu and import qgis into the notebook:

import qgis

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.