4

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 Jan 26 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). – Kadir Şahbaz Apr 19 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. – Kadir Şahbaz Apr 19 at 0:54
  • 2
  • 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. – ar-siddiqui Apr 19 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.