5

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.

6
  • Have you tried incorporating the Add Join tool in your code? However, it would be nicer if the MapDocument and Layer objects were join-aware.
    – PolyGeo
    Jul 23, 2013 at 22:48
  • @PolyGeo, the layers are already joined via the UI, and this is a process automation task where I am just checking for existence of joined fields, not adding the join. Jul 23, 2013 at 22:49
  • Might have to be an ArcGIS Idea (for ArcGIS Professional) I think - if you go that way post a link and I will vote for it.
    – PolyGeo
    Jul 23, 2013 at 22:51
  • 1
    I just made a little test and ran the exact code that you put up there (without lyr.dataSource) and it worked for me. I ran it inside PyScripter and it printed my two layers that I joined and for one of them it printed all its fields plus the fields from the join. I did create the join inside ArcMap and then saved and closed the MXD. Are you creating the join inside ArcMap or through Python code outside?
    – ianbroad
    Jul 25, 2013 at 16:40
  • @I-B, I followed all the same steps as you, with no luck. Created the join in ArcMap, using right-click on layer-> Joins & Relates-> Join GUI with the "Keep all records" option, and saving/closing ArcMap. What version and service pack of ArcGIS are you running? Jul 25, 2013 at 17:27

1 Answer 1

4

Sadly, I am going to have to chalk this one up to a bug that was introduced in Arc 10.0 SP1 or SP2.

From what I can tell, this bug was resolved in 10.1 (as @I-B suggested, and I confirmed). Why? Because I uninstalled SP2, SP3, SP4, & SP5 (I never installed SP1) all the way back to Arc 10.0 (no service packs), where the same exact code was finally able to run again.

To resolve this, I added the following code:

try:
    desc = arcpy.Describe(lyr)
except:
    #This is a workaround for Arc10.0 bug
    #This points back to the path/filename of the layer
    desc = arcpy.Describe(lyr.dataSource)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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