pythonqgispyqgis

Using QGIS in Python


I'm trying to use QGIS as my map provider in an Python GUI. However, I cannot get the app to launch.

I've created an script that launches the Python app with the correct environmental variables.

Write-Output "Setting up QGIS environment"

# Path to your QGIS installation
$QGIS_PREFIX_PATH = "C:\Program Files\QGIS 3.40.1"

# Set environment variables
$env:PATH = "$QGIS_PREFIX_PATH\bin;$QGIS_PREFIX_PATH\apps\qgis\bin;$QGIS_PREFIX_PATH\apps\Qt5\bin;$env:PATH"
$env:PYTHONPATH = "$QGIS_PREFIX_PATH\apps\qgis\python;$QGIS_PREFIX_PATH\apps\qgis\python\qgis\PyQt;$env:PYTHONPATH"
$env:GDAL_DATA = "$QGIS_PREFIX_PATH\share\gdal"
$env:QGIS_PREFIX_PATH = $QGIS_PREFIX_PATH
$env:QGIS_PATH = $QGIS_PREFIX_PATH
$env:QT_PLUGIN_PATH = "$QGIS_PREFIX_PATH\apps\Qt5\plugins"
$env:OSGEO4W_ROOT = $QGIS_PREFIX_PATH
$env:PATH = "$OSGEO4W_ROOT\apps\qgis\bin;$OSGEO4W_ROOT\apps\grass\grass78\lib;$env:PATH"

Write-Output "Running Python script"

# Run your Python script
python src/app.py

However, when running the app, I get the following import warning

Setting up QGIS environment
Running Python script
Could not find platform independent libraries <prefix>
Traceback (most recent call last):
  File "C:\Users\Tim\Documents\Projecten\Project\src\app.py", line 2, in <module>
    from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton, QCheckBox, QHBoxLayout, QScrollArea, QToolBar, QAction, QFileDialog
ModuleNotFoundError: No module named 'PyQt5.QtWidgets'

The import section looks like this

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton, QCheckBox, QHBoxLayout, QScrollArea, QToolBar, QAction, QFileDialog
from PyQt5.QtCore import QVariant
from qgis.core import (
    QgsApplication,
    QgsProject,
    QgsVectorLayer,
    QgsPointXY,
    QgsGeometry,
    QgsFeature,
    QgsField,
    QgsFields,
    QgsWkbTypes,
    QgsCoordinateReferenceSystem,
    QgsCoordinateTransformContext,
)
from qgis.gui import QgsMapCanvas, QgsMapToolPan, QgsMapToolZoom

QtWidgets.py is included with the QGIS installation in C:\Program Files\QGIS 3.40.1\apps\qgis\python\qgis\PyQt and is included in the PYTHONPATH variable, so I'm not sure what's going on here


Solution

  • It turns out I don't need that start script at all.

    I just needed to launch the app from the supplied shell OSGeo4W shell, which includes python.

    However, I did need to place a few imports at the top of the file.

    To give an complete example, using some imports;

    import os, sys
    sys.path.append(r'C:\\Program Files\\QGIS 3.40.1\\apps\\qgis\\python')
    sys.path.append(r'C:\\Program Files\\QGIS 3.40.1\\apps\\qgis\\bin')
    sys.path.append(r'C:\\Program Files\\QGIS 3.40.1\\apps\\Python37')
    sys.path.append(r'C:\\Program Files\\QGIS 3.40.1\\apps\\Python37\\Scripts')
    sys.path.append(r'C:\\Program Files\\QGIS 3.40.1\\apps\\Qt5\\bin')
    sys.path.append(r'C:\\Program Files\\QGIS 3.40.1\\bin')
    
    from qgis.PyQt.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton, QCheckBox, QHBoxLayout, QScrollArea, QToolBar, QAction, QFileDialog, QInputDialog
    from qgis.PyQt.QtCore import QVariant
    from qgis.PyQt.QtGui import QColor
    from qgis.core import (
        QgsApplication,
        QgsProject,
        QgsVectorLayer,
        QgsRasterLayer,
        QgsPointXY,
        QgsGeometry,
        QgsFeature,
        QgsField,
        QgsFields,
        QgsWkbTypes,
        QgsCoordinateReferenceSystem,
        QgsCoordinateTransform,
        QgsCoordinateTransformContext,
        QgsColorUtils
    )
    from qgis.gui import QgsMapCanvas, QgsMapToolPan, QgsMapToolZoom
    

    An even bigger pro-tip for VS Code users; put this in your workspace file and have the terminal pop up as a choice and proper language browsing.

    "settings": {
        "terminal.integrated.profiles.windows": {
            "OSGeo4W Shell": {
                "path": "c:\\Program Files\\QGIS 3.40.1\\OSGeo4W.bat"
            }
        },
        "terminal.integrated.defaultProfile.windows": "OSGeo4W Shell",
        "terminal.integrated.cwd": "${workspaceFolder}",
        "python.analysis.extraPaths": [
            "C:\\Program Files\\QGIS 3.40.1\\apps\\qgis\\python",
            "C:\\Program Files\\QGIS 3.40.1\\apps\\qgis\\bin",
            "C:\\Program Files\\QGIS 3.40.1\\apps\\Python37",
            "C:\\Program Files\\QGIS 3.40.1\\apps\\Python37\\Scripts",
            "C:\\Program Files\\QGIS 3.40.1\\apps\\Qt5\\bin",
            "C:\\Program Files\\QGIS 3.40.1\\bin",
        ]
    }