1

I'm running ArcGIS 10.3.1. and desire to list layers in the current mxd. I'm using the "CURRENT" method...

arcpy.mapping.MapDocument("CURRENT")

...within a python script tool, accessed via a single ArcMap session with the environment setting set to foreground processing. The usage is correct (identical to the help) and has worked for me last year. why would a foreground process fail to find the single mxd that it's being run from?

Link Esri ArcGIS 10.3 Help: Map Documents

enter image description here

    import arcpy
    mxd = arcpy.mapping.MapDocument("CURRENT")
    for lyr in arcpy.mapping.ListLayers(mxd):
        arcpy.AddMessage(lyr.name)
    del mxd
    arcpy.AddMessage(" ... current mxd ")

enter image description here

  • 1
    I think I know what is going on here but I think it will be much clearer if you can replace the picture of code with code as text that has been formatted using the {} button. However, can you also remove the try/except block from any code presented here because that masks the errors that Python provides to help you and us debug. – PolyGeo Mar 31 '16 at 7:23
  • Having the error message as text will be helpful to future searches like this one: gis.stackexchange.com/search?q=createobject+%5Barcpy%5D. – PolyGeo Mar 31 '16 at 8:12
3

Thinking of an earlier Q&A, I was going to say that the Python script tool is running in a separate process from ArcMap, but when I tested your code in ArcMap at ArcGIS 10.4 for Desktop it ran without a problem.

I think you should try closing ArcMap, and perhaps even rebooting, and then re-opening ArcMap to run your test again. This would eliminate the possibility that another process is somehow conflicting with yours.

Also, double-check that you have Run Python script in process checked. When I ran it as below I got an error, but it was different to yours.

enter image description here

Executing: test2 Start Time: Thu Mar 31 18:31:37 2016 Running script test2... ERROR 000714: Error in script test2. Error in executing: cmd.exe /C C:\Temp\test.py "#"

Failed to execute (test2). Failed at Thu Mar 31 18:31:48 2016 (Elapsed Time: 10.77 seconds)


Alternatively, as you have commented, and done successfully:

run a repair and remove your templates

i.e.

Problem solved: I deleted the normal template [(Normal.mxt)], created a new mxd, and the script worked as designed.

  • I had thought the same and had rebooted and repaired my ArcGIS Install. Bit it still had issues. I tested the "Run Python script in process" in both states, it was not fruitful in any case. I'll get a co worker to test it as well. – GISI Apr 1 '16 at 2:38
  • @GISI Is there any chance that you may be trying to run this tool from ArcCatalog (to save the overhead of ArcMap)? gis.stackexchange.com/questions/39119/… – PolyGeo Apr 1 '16 at 3:04
  • Nope. No chance at all, it's always run via ArcMap. – GISI Apr 1 '16 at 3:06
  • I've had a coworker run this and they did not have problems. Therefore it's a problem with my ArcGIS Desktop Install. Despite running repairs, I'm not sure how to fix it. – GISI Apr 1 '16 at 3:10
  • @GISI I wonder whether you are inadvertently running a different Python setup than what you think. I try to avoid playing around with PYTHONPATH etc but if you do a full uninstall of ArcGIS for Desktop, and remove any Python remnants, then reinstalling ArcGIS for Desktop with its Python may just fix it. – PolyGeo Apr 1 '16 at 3:14

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.