I have one script (and one alternative) that I want to use to replace a broken data source. Neither is working outside of the ArcMap Python Window.
Has it something to do with the MapDocument object?
Tested with a few different IDEs; including PyScripter, Pythonwin, PyCharm and IDLE.
In both instances the scripts run without error and print the correct print statements, but nothing is changed in the map document.
Only tested using Python 2.7.13 because when working with the ArcPy module and ArcMap, Python 3.x is not supported. It's supported in ArcGIS Pro.
No difference with mxd.saveACopy() instead of mxd.save()
Script using replaceDataSource()
mxd = arcpy.mapping.MapDocument(r'\\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_18\Test
Folder\Beecher_Unit_200U_B.mxd')
for lyr in arcpy.mapping.ListLayers(mxd):
if str(lyr.name) == "Test":
lyr.replaceDataSource(r"\\gisfile\GISstaff\Jared\Hydro.gdb", "FILEGDB_WORKSPACE", "Hydro_WillCounty_Waterways")
print(lyr.dataSource)
mxd.save()
del mxd
and using findAndReplaceWorkspacePaths()
mxd = arcpy.mapping.MapDocument(r'\\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_18\Test
Folder\Joliet_Elem_86_B.mxd')
mxd.findAndReplaceWorkspacePaths(r"\\gisfile\GISmaps\GISstaff\Jared\WillCoGIS_DataSHP.gdb", r"\\gisfile\GISstaff\Jared\Hydro.gdb")
mxd.save()
del mxd
On the other hand, these scripts work fine in the ArcMap Python window. For example,
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
if str(lyr.name) == "Test":
lyr.replaceDataSource(r"\\gisfile\GISstaff\Jared\Hydro.gdb",
"FILEGDB_WORKSPACE", "Hydro_WillCounty_Waterways")
print lyr.dataSource
mxd.save()
del mxd
I've had a look at similar posts, but I couldn't find one helpful enough: