Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active October 7, 2023 12:56
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ThomasG77/223064813d8aefda5b3cdb05c2588fa1 to your computer and use it in GitHub Desktop.
Save ThomasG77/223064813d8aefda5b3cdb05c2588fa1 to your computer and use it in GitHub Desktop.
Run PyQGIS in Jupyter Notebook

Run Jupyter notebook with PyQGIS

On Windows

Look at this existing tutorial https://lerryws.xyz/posts/PyQGIS-in-Jupyter-Notebook

On Ubuntu

Install

cd yourdir
python3 -m venv venv --system-site-packages
source venv/bin/activate
pip install -U notebook jupyterlab

Instructions to run the notebook

Run the virtual environment

cd yourdir
source venv/bin/activate
jupyter-lab

Then, create a Python 3 notebook (or open it if already existing), copy/ paste the following and run the notebook

import sys
import os

import qgis
from qgis.gui import *
from qgis.core import *
from qgis.utils import plugins
from PyQt5.QtCore import *
from qgis.analysis import QgsNativeAlgorithms

sys.path.append('/usr/share/qgis/python/plugins/')
sys.path.append('/usr/share/qgis/python/')

QgsApplication.setPrefixPath('/usr', True)
app = QgsApplication([], False)
app.initQgis()

import processing
from processing.core.Processing import Processing
Processing.initialize()

QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
for alg in QgsApplication.processingRegistry().algorithms():
        print(alg.id(), "--->", alg.displayName())

# Stop QGIS appllication
app.exitQgis()
app.exit()
@kevo619
Copy link

kevo619 commented Jun 3, 2022

Thank you so much

@Python-GIS-BG
Copy link

Thank you! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment