From cbe906c0c40f3c1b4456847f12fee43292342b76 Mon Sep 17 00:00:00 2001 From: nicogodet Date: Tue, 19 Nov 2024 11:03:49 +0100 Subject: [PATCH 1/4] add myself --- content/.authors.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/content/.authors.yml b/content/.authors.yml index 55a5b6a..15826bb 100644 --- a/content/.authors.yml +++ b/content/.authors.yml @@ -18,3 +18,9 @@ authors: name: Julien Moura slug: julien-moura url: https://geotribu.fr/team/julien-moura/ + nicogodet: + avatar: https://cdn.geotribu.fr/img/internal/contributeurs/ngo.webp + description: Hydraulic enthusiast + name: Nicolas Godet + slug: nicolas-godet + url: https://geotribu.fr/team/nicolas-godet/ From 9b8b4510f29463a0fed7b3fc449da9a2f70261ec Mon Sep 17 00:00:00 2001 From: nicogodet Date: Tue, 19 Nov 2024 11:16:42 +0100 Subject: [PATCH 2/4] Add pyqgis venv windows post --- ...-11-25_pyqgis_environnement_dev_windows.md | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 content/posts/2024-11-25_pyqgis_environnement_dev_windows.md diff --git a/content/posts/2024-11-25_pyqgis_environnement_dev_windows.md b/content/posts/2024-11-25_pyqgis_environnement_dev_windows.md new file mode 100644 index 0000000..d539450 --- /dev/null +++ b/content/posts/2024-11-25_pyqgis_environnement_dev_windows.md @@ -0,0 +1,108 @@ +--- +authors: + - nicogodet +categories: + - Posts +comments: true +date: 2024-11-25 +description: "For Intellisense happiness" +icon: material/microsoft-visual-studio-code +license: beerware +pin: false +tags: + - Plugin QGIS + - PyQGIS + - Python + - VS Code + - Windows +title: "Creating a Python Virtual Environment for QGIS Plugin Development with VS Code on Windows" +subtitle: Keep our env safe, for our PyQGIS children +--- + +# Creating a Python Virtual Environment for QGIS Plugin Development with VS Code on Windows + +## Introduction + +![PyQGIS logo](https://cdn.geotribu.fr/img/logos-icones/programmation/pyqgis.png){: .img-thumbnail-left } + +Anyone who has tried it knows that configuring a Python, PyQGIS, and PyQt environment on Windows for developing QGIS plugins is a real challenge. Often, it feels like a losing battle... + +Well, not anymore! After scouring the depths of the internet and exploring tips provided by [Julien](https://geotribu.fr/team/julien-moura/), here is one method to have (almost) all the auto-completions for PyQGIS, PyQt, and more in VS Code. + + + +[Leave a comment :fontawesome-solid-comments:](#__comments "Go to comments"){: .md-button } +{: align=middle } + +---- + +## Creating the Virtual Environment + +I will assume that you have installed QGIS in the `C:\OSGeo4W` directory (the procedure is the same whether QGIS is installed via the OSGeo4W network installer or the MSI package; the paths in this article just need to be adapted to your installation). + +1. Open an OSGeo4W Shell and navigate to the location where you want to create the virtual environment. + For example, a freshly created plugin template using the [QGIS Plugin Templater](https://gitlab.com/Oslandia/qgis/template-qgis-plugin). + +1. Run the following commands: + + ```cmd title="Creating a virtual environment in the OSGeo4W Shell" + C:\OSGeo4W\bin\python-qgis.bat -m venv --system-site-packages .venv + C:\OSGeo4W\bin\python-qgis.bat -c "import pathlib;import qgis;print(str((pathlib.Path(qgis.__file__)/'../..').resolve()))" > .venv\qgis.pth + ``` + + The `--system-site-packages` option allows the virtual environment to inherit libraries specific to the Python environment in QGIS. + +1. To ensure VSCode recognizes `processing` imports, add the following line to the `.venv\qgis.pth` file: + `C:\OSGeo4W\apps\qgis\python\plugins` + + Your file should look like this: + + ```text title="Contents of .venv\qgis.pth file" + C:\OSGeo4W\apps\qgis\python + C:\OSGeo4W\apps\qgis\python\plugins + ``` + + Make sure the encoding of the `.venv\qgis.pth` file is set to UTF-8. + +1. Create a `sitecustomize.py` file in the `.venv\Lib\site-packages` folder with the following content: + + ```python title=".venv\Lib\site-packages\sitecustomize.py" + import os + + os.add_dll_directory("C:/OSGeo4W/bin") + os.add_dll_directory("C:/OSGeo4W/apps/qgis/bin") + os.add_dll_directory("C:/OSGeo4W/apps/Qt5/bin") + ``` + +1. In the `.venv\pyvenv.cfg` file, modify the occurrences of `C:\OSGeo4W\bin` to `C:\OSGeo4W\apps\Python312`: + + ```ini title=".venv\pyenv.cfg" + home = C:\OSGeo4W\apps\Python312 + include-system-site-packages = true + version = 3.12.6 + executable = C:\OSGeo4W\apps\Python312\python.exe + command = C:\OSGeo4W\apps\Python312\python.exe -m venv --system-site-packages + ``` + +---- + +## In VS Code + +If you open VS Code in the folder where you just created the virtual environment, VS Code will automatically detect the environment (otherwise, install the [VS Code Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python)), and as you type code, VS Code will suggest PyQGIS objects or methods. + +![Import completion](https://cdn.geotribu.fr/img/articles-blog-rdp/articles/2024/pyqgis_environnement_dev_windows/vscode_intellisense_completion_imports.webp){: .img-center loading=lazy } + +![Method completion](https://cdn.geotribu.fr/img/articles-blog-rdp/articles/2024/pyqgis_environnement_dev_windows/vscode_intellisense_completion_methodes.webp){: .img-center loading=lazy } + +To also have all the completions related to PyQt, it seems necessary to install an additional Python library called `PyQt5-stubs` (although it is no longer maintained, it still works). +In the VS Code terminal, run the following command: + +```powershell title="Install PyQT completion in the virtual environment" +pip install PyQt5-stubs +``` + +![PyQt](https://cdn.geotribu.fr/img/articles-blog-rdp/articles/2024/pyqgis_environnement_dev_windows/vscode_pyqt.webp){: .img-center loading=lazy } + +All of this just to have colorful code :smiley:! + +![Contribute to GeoPF Altimétrie](https://cdn.geotribu.fr/img/articles-blog-rdp/articles/2024/pyqgis_environnement_dev_windows/vscode_geopf.webp){: .img-center loading=lazy } From da029e164c65977c512958ad5221f29a75062060 Mon Sep 17 00:00:00 2001 From: nicogodet Date: Tue, 19 Nov 2024 15:19:13 +0100 Subject: [PATCH 3/4] switch to Tutorial category --- content/posts/2024-11-25_pyqgis_environnement_dev_windows.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/posts/2024-11-25_pyqgis_environnement_dev_windows.md b/content/posts/2024-11-25_pyqgis_environnement_dev_windows.md index d539450..6dc02ca 100644 --- a/content/posts/2024-11-25_pyqgis_environnement_dev_windows.md +++ b/content/posts/2024-11-25_pyqgis_environnement_dev_windows.md @@ -2,7 +2,7 @@ authors: - nicogodet categories: - - Posts + - Tutorial comments: true date: 2024-11-25 description: "For Intellisense happiness" From 690a49070a579103e2d50f9a827bf2840958335b Mon Sep 17 00:00:00 2001 From: Nicolas Godet <39594821+nicogodet@users.noreply.github.com> Date: Mon, 25 Nov 2024 15:24:38 +0100 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Julien Signed-off-by: Nicolas Godet <39594821+nicogodet@users.noreply.github.com> --- ...024-11-25_pyqgis_environnement_dev_windows.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/content/posts/2024-11-25_pyqgis_environnement_dev_windows.md b/content/posts/2024-11-25_pyqgis_environnement_dev_windows.md index 6dc02ca..b86aced 100644 --- a/content/posts/2024-11-25_pyqgis_environnement_dev_windows.md +++ b/content/posts/2024-11-25_pyqgis_environnement_dev_windows.md @@ -8,6 +8,10 @@ date: 2024-11-25 description: "For Intellisense happiness" icon: material/microsoft-visual-studio-code license: beerware +links: + - "Cooking with Gispo: QGIS Plugin Development in VS Code": https://www.gispo.fi/en/blog/qgis-plugin-development-in-vs-code/ + - IDE settings for writing and debugging plugins (official PyQGIS cookbook): https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/plugins/ide_debugging.html + - French version: https://geotribu.fr/articles/2024/2024-11-25_pyqgis_environnement_dev_windows/ pin: false tags: - Plugin QGIS @@ -15,19 +19,19 @@ tags: - Python - VS Code - Windows -title: "Creating a Python Virtual Environment for QGIS Plugin Development with VS Code on Windows" +title: "Creating a Python virtual environment for PyQGIS development with VS Code on Windows." subtitle: Keep our env safe, for our PyQGIS children --- -# Creating a Python Virtual Environment for QGIS Plugin Development with VS Code on Windows +# Creating a Python virtual environment for developing QGIS plugin with VS Code on Windows ## Introduction ![PyQGIS logo](https://cdn.geotribu.fr/img/logos-icones/programmation/pyqgis.png){: .img-thumbnail-left } -Anyone who has tried it knows that configuring a Python, PyQGIS, and PyQt environment on Windows for developing QGIS plugins is a real challenge. Often, it feels like a losing battle... +Anyone who has tried it, knows that configuring a Python, PyQGIS, and PyQt environment on Windows for developing QGIS plugins is a real challenge. Often, it feels like a losing battle... -Well, not anymore! After scouring the depths of the internet and exploring tips provided by [Julien](https://geotribu.fr/team/julien-moura/), here is one method to have (almost) all the auto-completions for PyQGIS, PyQt, and more in VS Code. +Well, not anymore! After scouring the depths of the internet and exploring tips provided by [Julien](https://geotribu.fr/team/julien-moura/), here is one method to have (almost) all the auto-completions for PyQGIS, PyQt and more in VS Code. @@ -52,12 +56,12 @@ I will assume that you have installed QGIS in the `C:\OSGeo4W` directory (the pr The `--system-site-packages` option allows the virtual environment to inherit libraries specific to the Python environment in QGIS. -1. To ensure VSCode recognizes `processing` imports, add the following line to the `.venv\qgis.pth` file: +1. To ensure VS Code recognizes `processing` imports, add the following line to the `.venv\qgis.pth` file: `C:\OSGeo4W\apps\qgis\python\plugins` Your file should look like this: - ```text title="Contents of .venv\qgis.pth file" + ```text title="Content of .venv\qgis.pth file" C:\OSGeo4W\apps\qgis\python C:\OSGeo4W\apps\qgis\python\plugins ```