2

i want to turn on label using arcpy, i use current mxd and my mxd path in my script and my layers also active in the current MXD, but it give me this eror, can somebody help me?

here the script i used :

import arcpy

... mxd = arcpy.mapping.MapDocument(r"C:\Users\P51\Desktop\testingLABEL.mxd") 

... df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

... layer = arcpy.mapping.ListLayers(mxd, "point10", df)[0] 

... layer.showLabels = True

and this is the eror :

Runtime error Traceback (most recent call last): File "", line 4, in IndexError: list index out of range

here is the screenshot :

enter image description here

2

If you are doing this from the Python Window then instead of using:

mxd = arcpy.mapping.MapDocument(r"C:\Users\P51\Desktop\testingLABEL.mxd")

I think you should use:

mxd = arcpy.mapping.MapDocument("CURRENT")

My suspicion is that the layer named point10 has been added to your current map (which you presumably opened using C:\Users\P51\Desktop\testingLABEL.mxd) after you created your MapDocument object named mxd.

The IndexError seems to be saying that ArcPy cannot find a layer named point10 in the MapDocument object named mxd.


As an aside there is never a need to import arcpy in the Python Window of ArcMap. That is done automatically when the Python Window is opened.

  • oke thats great, its works, so the problem is in r"C:\Users\P51\Desktop\testingLABEL.mxd" and replace with "current", thanks anyway for your help – RPP Dec 16 '18 at 0:40

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.