5

I want to use PyQGIS function crs.isValid() in normal Python file outside of QGIS like this:

wkt = 'GEOGCS["WGS84", DATUM["WGS84", SPHEROID["WGS84", 6378137.0, 298.257223563]],' \
      'PRIMEM["Greenwich", 0.0], UNIT["degree",0.017453292519943295],' \
      'AXIS["Longitude",EAST], AXIS["Latitude",NORTH]]'
crs = QgsCoordinateReferenceSystem(wkt)
assert crs.isValid()

In the documentation it says that:

If you’re outside the PyQGIS console, the code snippets on this page need the following imports:

from qgis.core import (QgsCoordinateReferenceSystem,
                       QgsCoordinateTransform,
                       QgsProject,
                       QgsPointXY
                       )

But it doesn't mention how to install qgis.core to my Python. If I do something like pip install qgis I get this error message:

ERROR: Could not find a version that satisfies the requirement qgis (from versions: none) ERROR: No matching distribution found for qgis

Is there a way to use PyQGIS functions in a standalone Python file?

8
  • 2
    Which OS are you working on? Have you seen these topics 1, 2, 3, and 4.
    – Taras
    Commented Jan 26, 2021 at 6:11
  • 1
    The links in @Taras's comment contain the essential and sufficient instructions on how to run PyQGIS functions in a standalone Python file. So, if you add steps that you followed before running the standalone Python file to the question body, then we might offer a solution on where you did wrong. Otherwise, other answers which can be given here will not be different from those (in the links). Commented Apr 19, 2021 at 0:45
  • 2
    pip install qgis doesn't install pyqgis functions to the python. They are installed by QGIS installers (standalone installer or OSGeo network installer) in Windows. They are also not installed to Python's subfolders, but seperate QGIS subfolders. So you need to make a path to them. And the links in @Taras comment also explains how to do that. Commented Apr 19, 2021 at 0:54
  • 2
    Please review also these threads: (1) gis.stackexchange.com/a/277136/29431 (2) gis.stackexchange.com/q/362874/29431 (3) gis.stackexchange.com/q/256023/29431 Commented Apr 19, 2021 at 1:12
  • 1
    @simo I need to use pyQGIS outside of QGIS. CRS validation is just an example. I have got some answers in the comments, I will have a detailed look at them soon, they have useful material but some of them don't carry the context. I am still looking for an answer that explains at a beginner level, I believe this will be helpful for many people. Commented Apr 19, 2021 at 19:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.