I'm attempting to save a polygon created in a scratch layer to a shapefile. The Python code below is a simplified example. Note the questions: Saving layer as shapefile using PyQGIS and Setting QgsVectorFileWriter.SaveVectorOptions for QGIS 3 provided some of the help, which I'm including in this post for future reference,
# Attach libraries from qgis.core import * # attach main QGIS library from qgis.utils import * # attach main python library import os # attach operating system library # Set the working directory wd = "C:/test" # Set work directory os.chdir(wd) # Change the directory # Set a variable for the current project instance Prj = QgsProject().instance() # Object for current project # Save the project to this file name pnm = "Test.qgs" # Project file name pnm = wd + "/" + pnm # Concat. with path Prj.write(pnm) # Save the project # Create an array  object with the polygon vertices vrtcs =  vrtcs.append(QgsPointXY(396100,8969000)) vrtcs.append(QgsPointXY(396100,8973900)) vrtcs.append(QgsPointXY(397900,8973900)) vrtcs.append(QgsPointXY(397900,8969000)) # Create a polygon from the vertices ply_01 = QgsGeometry.fromPolygonXY([vrtcs]) # Create a feature object then append the polygon into ftr = QgsFeature() ftr.setGeometry(ply_01) print(ftr.geometry()) # Create a layer for the feature and add to the project lyr = QgsVectorLayer('Polygon?crs=epsg:29194','Test',"memory") Prj.addMapLayers([lyr]) # Make the layer editable, add the feature and save lyr.startEditing() lyr.addFeature(ftr) lyr.commitChanges() # Save as a shapefile Fl_ou = 'Test.shp' Fl_ou = wd + '/' + Fl_ou writer = QgsVectorFileWriter(lry, Fl_ou,'urf-8','ESRI Shapefile')
When I execute the last line I get the following error
"NameError: name 'lry' is not defined".
However, when I enter
type(lyr) into the console it recognizes the variable lyr as a
<class 'qgis._core.QgsVectorLayer'> so I am unsure why I'm getting the not defined error.