<div dir="ltr">I am having issues getting a shape file to load with the QgsVectorLayer function in PyQGIS. If I use the function in the python console within the QGIS GUI the layer loads fine and is valid. However, if I implement the same function in my standalone python 3 script, the layer.isValid() function returns False and the layer fails to load.<div><br></div><div><div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Consolas,"Courier New",monospace;font-size:14px;line-height:19px;white-space:pre"><div><span style="color:rgb(197,134,192)">import</span> sys, os, time</div><br><div>sys.path.extend([<span style="color:rgb(86,156,214)">r</span><span style="color:rgb(209,105,105)">'C:</span><span style="color:rgb(215,186,125)">\O</span><span style="color:rgb(209,105,105)">SGeo4W</span><span style="color:rgb(215,186,125)">\a</span><span style="color:rgb(209,105,105)">pps</span><span style="color:rgb(215,186,125)">\q</span><span style="color:rgb(209,105,105)">gis</span><span style="color:rgb(215,186,125)">\p</span><span style="color:rgb(209,105,105)">ython'</span>,<span style="color:rgb(86,156,214)">r</span><span style="color:rgb(209,105,105)">'C:</span><span style="color:rgb(215,186,125)">\O</span><span style="color:rgb(209,105,105)">SGeo4W</span><span style="color:rgb(215,186,125)">\a</span><span style="color:rgb(209,105,105)">pps</span><span style="color:rgb(215,186,125)">\P</span><span style="color:rgb(209,105,105)">ython37</span><span style="color:rgb(215,186,125)">\L</span><span style="color:rgb(209,105,105)">ib\site-packages'</span>])</div><br><div><span style="color:rgb(106,153,85)">#modify environment variables to find qgis and qt plugins during qgis.core import</span></div><div>os.environ[<span style="color:rgb(206,145,120)">'QT_QPA_PLATFORM_PLUGIN_PATH'</span>] = <span style="color:rgb(86,156,214)">r</span><span style="color:rgb(209,105,105)">'C:</span><span style="color:rgb(215,186,125)">\O</span><span style="color:rgb(209,105,105)">SGeo4W</span><span style="color:rgb(215,186,125)">\a</span><span style="color:rgb(209,105,105)">pps</span><span style="color:rgb(215,186,125)">\Q</span><span style="color:rgb(209,105,105)">t5</span><span style="color:rgb(215,186,125)">\p</span><span style="color:rgb(209,105,105)">lugins'</span></div><div><span style="color:rgb(106,153,85)">#os.environ['QT_PLUGIN_PATH'] = r'%QT_PLUGIN_PATH%;C:\OSGeo4W\apps\Qt5\plugins;C:\OSGeo4W\apps\qgis\qtplugins'</span></div><div>os.environ[<span style="color:rgb(206,145,120)">'PATH'</span>] += <span style="color:rgb(86,156,214)">r</span><span style="color:rgb(209,105,105)">';C:</span><span style="color:rgb(215,186,125)">\O</span><span style="color:rgb(209,105,105)">SGeo4W</span><span style="color:rgb(215,186,125)">\a</span><span style="color:rgb(209,105,105)">pps</span><span style="color:rgb(215,186,125)">\q</span><span style="color:rgb(209,105,105)">gis\bin;C:</span><span style="color:rgb(215,186,125)">\O</span><span style="color:rgb(209,105,105)">SGeo4W</span><span style="color:rgb(215,186,125)">\a</span><span style="color:rgb(209,105,105)">pps</span><span style="color:rgb(215,186,125)">\Q</span><span style="color:rgb(209,105,105)">t5\bin'</span></div><br><div><span style="color:rgb(197,134,192)">from</span> qgis.core <span style="color:rgb(197,134,192)">import</span> *</div><div><span style="color:rgb(197,134,192)">from</span> qgis.gui <span style="color:rgb(197,134,192)">import</span> *</div><br><div><span style="color:rgb(106,153,85)"># supply path to qgis install location</span></div><div>QgsApplication.setPrefixPath(<span style="color:rgb(86,156,214)">r</span><span style="color:rgb(209,105,105)">'C:</span><span style="color:rgb(215,186,125)">\O</span><span style="color:rgb(209,105,105)">SGeo4W</span><span style="color:rgb(215,186,125)">\a</span><span style="color:rgb(209,105,105)">pps</span><span style="color:rgb(215,186,125)">\q</span><span style="color:rgb(209,105,105)">gis'</span>, <span style="color:rgb(86,156,214)">True</span>)</div><div><span style="color:rgb(106,153,85)">#QgsApplication.setPluginPath('C:\\OSGeo4W\\apps\Qt5\\plugins\\platforms')</span></div><div><span style="color:rgb(106,153,85)">#print(QgsApplication.systemEnvVars())</span></div><br><div><span style="color:rgb(106,153,85)"># create a reference to the QgsApplication</span></div><div><span style="color:rgb(106,153,85)"># setting the second argument to True enables the GUI, which we need to do</span></div><div><span style="color:rgb(106,153,85)"># since this is a custom application</span></div><div>qgs = QgsApplication([], <span style="color:rgb(86,156,214)">True</span>)</div><br><div><span style="color:rgb(106,153,85)"># load providers</span></div><div>qgs.initQgis()</div><br><div><span style="color:rgb(106,153,85)">##########################</span></div><div><span style="color:rgb(106,153,85)"># Write your code here to load some layers, use processing algorithms, etc.</span></div><div>canvas = QgsMapCanvas()</div><div>canvas.show()</div><div>layer = QgsVectorLayer(<span style="color:rgb(86,156,214)">r</span><span style="color:rgb(209,105,105)">'C:</span><span style="color:rgb(215,186,125)">\U</span><span style="color:rgb(209,105,105)">sers</span><span style="color:rgb(215,186,125)">\M</span><span style="color:rgb(209,105,105)">att</span><span style="color:rgb(215,186,125)">\O</span><span style="color:rgb(209,105,105)">neDrive</span><span style="color:rgb(215,186,125)">\F</span><span style="color:rgb(209,105,105)">armProject</span><span style="color:rgb(215,186,125)">\K</span><span style="color:rgb(209,105,105)">ankakee_Parcels</span><span style="color:rgb(215,186,125)">\K</span><span style="color:rgb(209,105,105)">3_TaxParcels.shp'</span>, <span style="color:rgb(206,145,120)">'Kankakee'</span>, <span style="color:rgb(206,145,120)">'ogr'</span>)</div><div><span style="color:rgb(220,220,170)">print</span>(layer.attributeList())</div><div><span style="color:rgb(197,134,192)">if</span> <span style="color:rgb(86,156,214)">not</span> layer.isValid():</div><div>  <span style="color:rgb(220,220,170)">print</span>(<span style="color:rgb(206,145,120)">'Failed to open the layer'</span>)</div><br><div><span style="color:rgb(106,153,85)"># add layer to the registry</span></div><div>add_layers = QgsProject.instance().addMapLayer(layer)</div><br><div><span style="color:rgb(106,153,85)"># set extent to the extent of our layer</span></div><div>canvas.setExtent(layer.extent())</div><br><div><span style="color:rgb(106,153,85)"># set the map canvas layer set</span></div><div>canvas.setLayers([add_layers])</div><div>canvas.refresh()</div><div>time.sleep(<span style="color:rgb(181,206,168)">30</span>)</div><div><span style="color:rgb(106,153,85)">########################</span></div><br><div><span style="color:rgb(106,153,85)"># When your script is complete, call exitQgis() to remove the provider and</span></div><div><span style="color:rgb(106,153,85)"># layer registries from memory</span></div><div>qgs.exitQgis()</div></div></div></div>