The following code will work if I run it from the ArcMap Python dialog, or from a GP tool, but not from PyScripter IDE:
import arcpy
mxd = arcpy.mapping.MapDocument(r"c:\temp\<my_mxd_name>.mxd")
layerList = arcpy.mapping.ListLayers(mxd)
for lyr in layerList:
print "Layer = " + lyr.name
#Get all fields in the layer
fList = arcpy.Describe(lyr).fields
for f in fList:
print "Field = " + f.baseName
When I run the exact same code from PyScripter, it fails on arcpy.Describe(lyr).fields
with the error IOError: "<TOC_layer_name>" does not exist
, even though it does exist in the referenced MXD table of contents. When PyScripter fails it opens up the ESRI file _base.py
to the describe
function. I am assuming there must be something different about describing a layer in ArcMap vs calling it directly from Python, but cannot figure out what it could be.
How do I properly use the arcpy.Describe() function on a layer when I am not running the script directly from ArcMap?
I have figured out that if I use arcpy.Describe(lyr.dataSource)
it functions properly, but only returns the fields from the source data layer. However, since I'm trying to read fields from a joined table (which does work in the original code above when running from AcrMap), I really need to see the fields from all joined tables, not just the data source.