Select to view content in your preferred language

lyr.replaceDataSource Question

06-16-2017 01:08 PM
Occasional Contributor III

Hello, I have the following script:

import arcpy
fileno = arcpy.GetParameterAsText(0)
#Define the path of the mxd
mxd_file = 'o:/plan/subdiv/' + fileno + '/' + fileno + '-ortho_photo.mxd'
arcpy.AddMessage('fileno: ' + fileno + ' mxd_file: ' + mxd_file)
mxd = arcpy.mapping.MapDocument(mxd_file)
for lyr in arcpy.mapping.ListLayers(mxd):
    if == 'roads':
    elif == 'railways':
    elif == 'rivers':
    elif == 'lakes':
    elif == 'proposed_OUTLINE_PLAN_polygon - OP item Site Polygon':
        lyr.replaceDataSource('O:/plan/subdiv/' + fileno + '/data_for_item.gdb','FILEGDB_WORKSPACE','proposed_OUTLINE_PLAN_polygon - OP item Site Polygon')
#Remove layer from ortho_photo.mxd
for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
        if == 'site_polygon':
            arcpy.mapping.RemoveLayer(df, lyr)
del mxd
Result = True

At line 17 I get the following error when executing:

Traceback (most recent call last):

File "X:\GIS\LUAM\DEV\Scripts\OtherTypes\", line 17, in <module>

lyr.replaceDataSource('O:/plan/subdiv/LOC2018-0001/data_for_item.gdb','FILEGDB_WORKSPACE','proposed_OUTLINE_PLAN_polygon - OP item Site Polygon')

File "c:\program files (x86)\arcgis\desktop10.2\ArcPy\arcpy\", line 181, in fn_

return fn(*args, **kw)

File "c:\program files (x86)\arcgis\desktop10.2\ArcPy\arcpy\", line 680, in replaceDataSource

return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))

ValueError: Layer: Unexpected error

The layer 'proposed_OUTLINE_PLAN_polygon - OP item Site Polygon' is part of a feature dataset called 'data', so I don't know if that may have anything to do with it? Although, I did read that if you are using this command with a feature dataset to just reference the geodatabase and it should work fine.

Right now I'm at a bit of a loss regarding this "Unexpected error". Any ideas appreciated. Thanks!

0 Kudos
4 Replies
MVP Emeritus

I'm not near a machine where I can test, but a couple thoughts.

first, you may want to set a variable and print out  'O:/plan/subdiv/' + fileno + '/data_for_item.gdb'

im also wondering if the path is too long?  Can you test by renaming to a short name and test?

i have an addin /blogs/myAlaskaGIS/2015/08/31/python-addin-for-data-inventory-and-broken-link-repair?sr=search&searc...‌  that might help you.  

Occasional Contributor III

Hi Rebecca, when setting the path to a variable it does print out correctly. Also, the long path does not have issues in other scripts.

I`m wondering if this could have to do with the fact that I have a different mxd open when this script is run which references the `ortho photo mxd`. Both mxds reference the same data_for_item.gdb, wondering if it could be some sort of locking issue.

I'm thinking maybe I'll move this script and have it run when the user has the ortho photo mxd open instead. Couple extra mouse clicks for the user, but probably cleaner in the end.


0 Kudos
MVP Emeritus

can you print out your getparameterastext(0) from line 1 and 17, to check to see if it is path compliant

Occasional Contributor III

Hi Dan, They both print the same and are correct. Thanks for the input.


0 Kudos