[Python] Using QGIS in Python

Discussão em 'Python' iniciado por Stack, Novembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página