I am rewriting a 2.x script for compatibility with ArcGIS Pro and Python 3.x. My question is, how do I export a shapefile from a hosted feature layer from ArcGIS online using ArcPy (Python 3.x) and ArcGIS Pro?

The working python 2.x script written for ArcGIS Desktop does it this way:

mxd = arcpy.mapping.MapDocument("path_to_mxd_containing_online_layer.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0] 

### exporting the data to a local TEMP layer
inspector_layer = arcpy.mapping.ListLayers(mxd, "name_of_layer_in_mxd", df)[0]
for lyr in arcpy.mapping.ListLayers(mxd, '', df):
    if lyr.isFeatureLayer == True:
        d = lyr.name.encode('ascii', 'ignore')
        file_name = d + ".shp"
        dest1 = os.path.join(wkspce, file_name)
            arcpy.CopyFeatures_management(lyr, dest1)

I was thinking of creating an aprx project file that contained a map with the layer in it, as that seems the closest to the original script. This is one way of writing it:

aprx = arcpy.mp.ArcGISProject("G:\\Code Enforcement\\ACD Admin Operations\\GIS\\Operations\\Mapping\\Automation\\Auto_District_Mapping_PRO\\Auto_District_Mapping_PRO.aprx")
m = aprx.listMaps("name_of_map")
inspector_layer = m.listLayers('name_of_layer')

arcpy.CopyFeatures_management(inspector_layer, r"C:\\testfolder\\name_of_layer.shp")

I am getting the following error: 'list' object has no attribute 'listLayers' on the line that reads inspector_layer = m.listLayers('name_of_layer'), hence my question of how to do it correctly.

1 Answer 1


Using addDataFromPath function can add hosted feature layers using the service url.

The code looks like: inspector_layer = m.addDataFromPath('service_url_for_hosted_feature_layer')

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.