This question already has an answer here:
I am trying to create a standalone application using Python 3.7 and QGIS3.6. on Windows 10. I have installed QGIS using the OSGEO4W method, in the 64-bit flavour.
My issue is that when I try to load a layer, it always says that the layer is not valid, as determined by
layer.isValid() = False.
current paths are:
I have read several answers on this, but none of them have worked for my so far; mostly the answers have been to do with changing setPrefixPath, which I have done in several different ways to no avail. I have also changed the pythonpath, but that did not work either.
What else can I try?
import os, sys, time, csv os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'c:\osgeo4w64\apps\Qt5\plugins' os.environ['PATH'] += r';c:\osgeo4w64\apps\qgis\bin;c:\osgeo4w64\apps\Qt5\plugins' sys.path.extend([r'c:\osgeo4w64\apps\qgis\python',r'c:\osgeo4w64\apps\Python37\Lib\site-packages']) from qgis.core import * from qgis.gui import * from qgis.utils import * from qgis.core import QgsProject from PyQt5.QtCore import * from PyQt5.QtGui import * # Instantiate the QGIS Application app = QgsApplication(, False) # second argument to False disables the GUI # Update prefix path app.setPrefixPath("C:/OSGeo4W64/apps/qgis", True) app.initQgis() #print (QgsApplication.showSettings()) # File to study area (vector) vector_source = "path/to/inputs StudyArea.shp" # Read layer from file layer = QgsVectorLayer(vector_source, "Vector_Layer", "ogr") # Test if the file path points to a valid file print ("File Path points to a file: ", os.path.isfile(vector_source)) # Test if the layer is valid print ("Output of layer.isValid(): ", layer.isValid())
I finally found the answer by changing by setting the
os.environ['QGIS_PREFIX_PATH']. See @Matt Brauer’s answer to PyQGIS QgsVectorLayer() Loads Invalid Layer in Standalone Python 3 Script