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
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
arcpy.AddMessage(lyr.name)
del mxd
arcpy.AddMessage(" ... current mxd ")
1 Answer 1
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.
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– GISI2016年04月01日 02:38:54 +00:00Commented Apr 1, 2016 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/…2016年04月01日 03:04:44 +00:00Commented Apr 1, 2016 at 3:04
-
Nope. No chance at all, it's always run via ArcMap.GISI– GISI2016年04月01日 03:06:42 +00:00Commented Apr 1, 2016 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– GISI2016年04月01日 03:10:53 +00:00Commented Apr 1, 2016 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.2016年04月01日 03:14:45 +00:00Commented Apr 1, 2016 at 3:14
{}
button. However, can you also remove thetry/except
block from any code presented here because that masks the errors that Python provides to help you and us debug.