I've checked the other answers here and none of the answers worked for me...

It's a "simple" matter of finding a layer by name in a map and changing it's datasource.

Here's my script:

    #-- New Source
    newSource = os.path.join(currDir, lyrZones + "_Dep" + dirNum + ".shp")

    #-- Validation
    if arcpy.Exists(newSource):
        print "The new source exists!"
        print "Not found!"

    #-- Find the layer and change its source            
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == lyrZones:
            print "Layer found!"
            print layers.dataSource
            print "  Replacing source..."
            lyr.replaceDataSource(currDir, "SHAPEFILE_WORKSPACE", newSource)
            print lyr.dataSource

I run it from a python script outside ArcGIS.

enter image description here

You can see that the source exists, the layer is found, I can access it's datasource by printing it, but the lyr.replaceDataSource causes an error (Unexpected error... doesn't help much...)

I've been working at it for 4-5 hours now and I'm short of solutions to try. Any idea what can cause the crash?

  • 1
    On first glance, it looks like you've got some odd characters in your directory path. Try moving the shapefile to a test directory like D:\test and see if that works.
    – Mintx
    Nov 20, 2012 at 15:45
  • Never had any problem with that before. The character is "#" and it's only an old sorting trick so my projects will appears first. I tried putting my project in D:/TEMP, but it didn't work...
    – fgcarto
    Nov 20, 2012 at 16:03

4 Answers 4


I know this is pretty old, but I just encountered this same issue and was surprised by the lack of solutions out there. The solution is so simple, but you'd have no way of knowing it based on ESRI's practically non-existent help:

Don't include the shapefile extension. ...That's it.

newSource = os.path.join(currDir, lyrZones + "_Dep" + dirNum + ".shp")

Should read:

newSource = os.path.join(currDir, '{}_Dep{}'.format(lyrZones, dirNum))
  • 2
    The question and answer concerns shapefiles. However I encountered a similar issue with a feature class in an Enterprise Geodatabase (aka SDE). It turns out arcpy.Exists requires a fully qualified name. However, the layer.replaceDataSource fails if you use a fully qualified name, but works if you only use feature name and leave out the database and ownername.
    – dvdhns
    Sep 25, 2015 at 22:39

I don't know if this helps you or not but I've encountered this error trying to switch SDE data sources for a layer. What I found is that when I'd run from PyScripter, it would spool up a 64-bit python.exe process. I'd get the ValueError. If I forced the script to run using the 32-bit python.exe via Command Prompt, I wouldn't get the error. I don't know if that would help your issue or not but it's worth a shot!

  • Thanks. It's all 32-bit, so it can't be the issue, but I'll keep that in mind when I'll try 64-bit geoprocessing. For now, I've left that part out of my script and replaced the source by hand... :(
    – fgcarto
    Mar 8, 2013 at 12:57

Most info on this is here. But to solve this I went into the python window within ArcCatalog to check on the ESRI definitions of each parameter in the .replaceDataSource function.

.replaceDataSource(workspace, workspace_type, dataset_name)

workspace = workspace of new data source

workspace_type = the actual workspace type listed in the replace datasource section of the linked help.

dataset_name = file name without the file type (IE: .shp), not the dataset name in the table of contents. No need for the directory in front of it either.


Fun with debugging! Try this:

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name == lyrZones:
        if lyr.supports("DATASOURCE"):
            print lyr.dataSource
            print currDir
            print newSource
            lyr.replaceDataSource(currDir, "SHAPEFILE_WORKSPACE", newSource, TRUE)
            print "Did we crash yet?"
            print lyr.dataSource
            print "Layer isn't supported!"
  • At least I know Datasource is supported. Got the same error message. Crashed on "lyr.replaceDataSource(currDir, "SHAPEFILE_WORKSPACE", newSource, True)". Didn't get to "Did we crash yet?". Frustrating...
    – fgcarto
    Nov 20, 2012 at 17:07
  • Argh, that sucks. What's the end goal you're trying to achieve? Maybe there's another way to go about this.
    – Mintx
    Nov 20, 2012 at 17:17
  • I have 5 templates and between 14 and 50 maps of each to do (128 maps total). I have two layers to update on each one. The first is only a Def. Query which I handled like a boss ;) (lyr.definitionQuery = defQueryLines). The other is a polygon layer that has a graduated color symbology, but the file has to change. I wrote a script that creates an output for each map and I need to update the source and change the symbology value (but I think it's 10.1 that do that. I have 10.0) so I could keep the symbology...
    – fgcarto
    Nov 20, 2012 at 18:12
  • For now, I update the file manually (the first batch has 20 maps so it's not that tedious), but the next one will have 50 maps so...
    – fgcarto
    Nov 20, 2012 at 18:13
  • Does the tool has to work with a .lyr file? Maybe I could work directly on a .lyr where I could change is datasource. I don't see any properties to change the symbology valye (the field symbolized though...)
    – fgcarto
    Nov 20, 2012 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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