2

Using Python for QGIS in a Jupyter Notebook, how can I automate the task of

  • importing a dxf file
  • getting a data web service as a layer ?

I have tried (https://lerryws.xyz/posts/Install-Jupyter-Notebook-in-QGIS3) which was the only source I found in order to install Jupyter Notebook in QGIS. However, the tutorial asks to make a batch file and store it in C:\OSGeo4W64\bin, which is something that is missing for me.

If anybody knows a better or easier way to use QGIS with Jupyter notebook, please let me know.

I am using a Windows PC and my QGIS version is 3.20.1.

6
  • What is your main question? Accessing QGIS using Jupyter or automating the tasks? Sep 14, 2021 at 13:05
  • I know you particularly want to use a Jupyter Notebook, however, I would suggest that the easiest way to get started with Python in QGIS is with the built-in script editor. The PyQGIS API is fully accessible and you don't need to worry about installing these extra components.
    – Matt
    Sep 14, 2021 at 13:06
  • You can also have a look to this repository : github.com/3liz/qgis-nbextension
    – etrimaille
    Sep 14, 2021 at 17:04
  • @KadirŞahbaz, the main question is to Access QGIS using Jupyter. Sep 21, 2021 at 12:31
  • @Matt Thank you Sep 21, 2021 at 12:35

2 Answers 2

1
  1. Install QGIS Standalone (any version). I'm showing this on Windows.
  2. Open the OSGeo4W.bat found in the QGIS install folder (i.e. "C:\Program Files\QGIS 3.32.3\OSGeo4W.bat"). This is necessary so that all the environment variables are set to run the next commands so that everything is installed on the version of Python that is built into QGIS.
  3. Run, "python -m pip install jupyter"
  4. Using File Explorer, get the path to the "python-qgis.bat" file (i.e. "C:\Program Files\QGIS 3.32.3\bin\python-qgis.bat")
  5. In the OSGeo4W.bat window that you opened in step 2, navigate to the folder you want to run or save the IPYNB file. (i.e. "cd /d D:\test_folder")
  6. Using the path to the "python-qgis.bat", run python and open Jupyter Notebook. "C:\Program Files\QGIS 3.32.3\bin\python-qgis.bat" -m notebook

Now you have Jupyter running with access to PyQGIS and all the libraries installed there. To get the debugging of a Python IDE like PyCharm Pro, you'll need to use the instructions that show up after running the last command. Specifically you'll need the "localhost" url and "token". Specific instructions for this should be in your IDE's help, enter image description here

EDIT: In response to @Khaled, yes, this is the sript you'd save in a batch file in the folder you want to start a Notebook. (Change the path to OSGeo4W.bat)

"cd /d %~dp0"
"C:\Program Files\QGIS 3.32.3\OSGeo4W.bat" python-qgis -m notebook
pause
1
  • 1
    +1. Your answer is wonderful, I would ask you, can I for example create some batch file like "open_notebook_here.bat" and if I run this batch file then it does all what you said and open notebook in current directory where this "open_notebook_here.bat" found?
    – Khaled
    Mar 14 at 16:51
0

One way to run jupyter with QGIS is to use a conda environment to install both.

  1. Install mini-forge

  2. Using the installed miniforge prompt, create an environment that provides both qgis and jupyter notebook:

    mamba create -n qgis_jupyter qgis notebook
    

    You can provide the version of QGIS you want to use:

    mamba create -n qgis_jupyter qgis=3.28.13 notebook
    
  3. activate the qgis_jupyter environment:

mamba activate qgis_jupyter
  1. Launch jupyter
jupyter notebook

You can now import qgis modules within the notebook.

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.