I have a layer in my gdb file called lines. I also have a *.lyr file lines-N with symbology for the lines layer. I need lines-N to have the same data source as lines, using arcpy.replaceDataSource.

My code:

desc=arcpy.Describe ("lines")
path = desc.path
for lyr in arcpy.mapping.ListLayers(mxd, "",df):
      if lyr.name == "lines-N":
          if lyr.supports ("DATASOURCE"):
              lyr.replaceDataSource (path,"FILEGDB_WORKSPACE","",True)

produces this error:

return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))
ValueError: Layer: Unexpected error

What am I doing wrong? I'm trying to avoid manually adding the file path like so:

lyr.replaceDataSource (r "D:\Folder\file.gdb","FILEGDB_WORKSPACE","",True)

I'm running it from the ArcMap Python window to test it first, and then adding it to a script that performs similar operations on other layers.
ArcGIS version is 10.2.2

  • 1
    How do you run your code? (from Python IDE, from Python window in ArcMap, as script tool?). I have a snippet of code that replicates yours and it works fine. Please update the question with this info + version of ArcGIS you have. One thing I strongly suggest avoiding - using strings for layer names in arcpy.Describe. There can be multiple feature layers created behind the scenes with various data sources, you can spend hours debugging. A better way - cities_lyr = [lyr for lyr in mp.ListLayers(mxd,'cities')][0] and then path = arcpy.Describe(cities_lyr).path Dec 2, 2016 at 10:48
  • @AlexTereshenkov, Just updated info as you requested. By the way, I have found that path = desc.path makes path of dataset if it exists, not just *.gdb file. Probably this caused error. Anyway you told that you've got a snippet, should it help in my case? Dec 2, 2016 at 12:09
  • The path should contain only the folder/gdb, without the dataset name. Dec 2, 2016 at 12:35

1 Answer 1


Try running this piece of code in Python window in ArcMap. You should have two layers - lines and lines-N in an ArcMap map document. They should refer to a feature class with the same name with each of the feature classes stored within two separate file geodatabases:

import arcpy
import arcpy.mapping as mp

mxd = mp.MapDocument(r'current')
lines_lyr = [lyr for lyr in mp.ListLayers(mxd,'lines')][0]
path = arcpy.Describe(lines_lyr).path
for lyr in mp.ListLayers(mxd,""):
    if lyr.name == 'lines-N':


After running this, you should immediately see that the lines-N map layer has now as the data source the file geodatabase used for the map layer lines.

  • The same error I mentioned before, path tool calculates path to to dataset, not gdb. It could be right to use string scripts to delete one word after the last "\", but the thing is that feature classes can be stored directly in the root of gdb file. Dec 2, 2016 at 12:49
  • Just found out! I used that: pathgdb= path.split ("gdb")[0] + 'gdb' and replaced it in your replaceDataSource Dec 2, 2016 at 13:04

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.