I have an .mxd that I use as a template in ArcMap 10.8. I currently manually update the data source for certain layers in the MXD. I am trying to create an arcpy script to do this for me but am stuck at the part of how to change the data source for these layer(s).

I believe that I need to first determine the source of the layers I am changing and then use that to change the other layers with that same source. I need to do this as sometimes my template sources get changed by accident so I cannot put a set path in my script. I am just unsure of how to determine the source of a layer and how to change it and the source of the other layers in my MXD that have the same found source.

Here is a picture of the layers I change.


The application Tenure layer is a polygon fc and tenure road application layer, Pofc and PofT layers (all have the same source) are line fc's. If someone could just help me with the code to find the source of one of the layers, I can figure out the loop to select which layer to find the source from. And I could also use some help on how to replace the source of other layers with that source. I believe I have to use:

Layer.replaceDataSource(workspace_path, workspace_type, dataset_name, {validate})

If yes, I am unsure of how to use it.

  • 1
    The Esri documentation has nine examples. Please Edit the question to state what you have tried and the error message or other negative outcome you experienced.
    – Vince
    Apr 1, 2021 at 19:52
  • What's the version of Python you're using? If you're on arcgis-10.8 then looks like you're using Python 2.7. support.esri.com/en/technical-article/000013224
    – Pfalbaum
    Apr 1, 2021 at 21:17
  • Also, do you want your script to be stand-alone or are you working in an ArcMap Python Window?
    – Pfalbaum
    Apr 1, 2021 at 21:25
  • Something else to think about, do you have multiple MXDs that you're working with or are you doing one at a time?
    – Pfalbaum
    Apr 1, 2021 at 21:30
  • Yes, The server our arcmap is on uses pyghon 2.7 and I am using 10.8 arcmap, I am creating a stand alone scrip that I can run everytime I have to create one of these maps. sometimes it is multiple times a day. Just one MXD and I 'save as' once I change the sources for the layers that are the focus of the map.
    – BCGeoGirl
    Apr 1, 2021 at 21:47

2 Answers 2


You could try this to get the data source of the layers in your MXD:

import arcpy

mxd = arcpy.mapping.MapDocument("MXD PATH HERE")
layers = arcpy.mapping.ListLayers(mxd)

for layer in layers:
    if layer.supports("dataSource"): #not all layers are supported (see help doc)
        print layer.dataSource #returns complete path for layers data source

Or if you want to get a specific layer in the dataframe you could reference this answer.

mxd = arcpy.mapping.MapDocument("MXD PATH HERE")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    if lyr.name == "Tim":
        # Do some stuff.

In theory, once you have your specified layer in a variable you could then use it in findAndReplaceWorkspacePath

findAndReplaceWorkspacePath (find_workspace_path, replace_workspace_path, {validate}).

I've moved on to Python 3.x a couple years ago.

  • Thank you, for your response. There is definitely useful information in there. I apologize for using 2.7 it is just what our arcmap server has on it. My problem lies in determining the path of the source for specific layers in my mxd. The source for each of these layers is the same, just the layer names are different. I was hoping to find a way to determine what the path of the source for these specific layers in the mxd are so that I can assign it to a variable and change it to the new source. Otherwise it is going to create a lot more coding to get my maps to look the same.
    – BCGeoGirl
    Apr 6, 2021 at 15:17
  • Have you tried any of the above?
    – Pfalbaum
    Apr 6, 2021 at 19:20
  • I am still working on them. I am pretty new to python and am unsure exactly what some of the things are and am playing around with it. I am unsure how to isolate the source for the layers I need to find the source for. sometimes I save my template instead of save as and the source changes so I cannot put in a fixed source into the code. I do clearances for licensing so the if is the information for one part of the mxd that changes depending on the type of license it is (road or polygon). I will post again when I figure out how to work these.
    – BCGeoGirl
    Apr 7, 2021 at 15:11

With help from answers/comments I was able to muddle through this. Without showing my whole script I have the part related to what I was asking. I also managed to save the mxd as a new copy with the new source file and it worked.

mxd = arcpy.mapping.MapDocument(r"C:\KVidal\2021_10.8_ExA_Portrait.mxd") #just for testing purposes
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    if permitType == "N":
        if lyr.name == "Application Tenure":
            lyr.replaceDataSource(gdb, "FILEGDB_WORKSPACE", newFC, True) #changing the source for a layer in my mxd
        if lyr.name in ("Tenure Road Application", "PofC", "PofT"):
            lyr.replaceDataSource(gdb, "FILEGDB_WORKSPACE", newFC, True)

arcpy.env.workspace = workFol
mxdName = gdb + "_ExA.mxd"

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.