Thursday 4 July 2019

arcgis desktop - foreground arcpy.mapping.MapDocument("CURRENT") in python script in single ArcMap produces createobject errors


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



Answer



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.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...