Error when running arcpy.mapping code as toolbox script

2792
7
02-02-2011 04:35 PM
ScottOatley
New Contributor II

Hello,

The following code works fine when I copy and paste it to the python command window.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
df.name



It produces the following errors when I run the script in the Python shell (F5) and similar errors if I try to use it as a script from a toolbox.

Traceback (most recent call last):
File "C:\gisdata\python\test.py", line 3, in <module>
mxd = arcpy.mapping.MapDocument("CURRENT")
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\ArcPy\arcpy\arcobjects\mixins.py", line 441, in __init__
super(MapDocumentMethods, self).__init__(mxd)
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\ArcPy\arcpy\arcobjects\_base.py", line 47, in __init__
for arg in args))
RuntimeError: Object: CreateObject cannot open map document


Is there something special I need to add/setup etc, to run in those other modes?

Never mind. I figured out that I didn't have the "Always run in foreground" check box selected as needed when using "CURRENT".

Thanks,
Scott

Tags (2)
7 Replies
FreddieGibson
Regular Contributor II
Although you identified that script tools using the CURRENT keyword can only be run in the foreground, please note that when creating a MapDocument object you will only want to ONLY use the CURRENT keyword if you're working from within ArcMap via a Script Tool or the Python window.  If you attempt to create a MapDocument object outside of ArcMap (i.e. ArcCatalog, a Python IDE, etc.) with the CURRENT keyword you will receive a similar runtime error.  When working in an environment outside of ArcMap you will want to reference the file path to the mxd instead of using the CURRENT keyword.
RaresRotar
New Contributor II

Thank you for the clarification, concerning CURRENT and REFERENCE FILE PATH!

0 Kudos
krishmandali
New Contributor

@Freddie Gibson, I am writing a small automation to create a view, register the view in Geo database and publish it as a service. So far good with creating a view and register with Geo database but I am trying to save the mxd before publishing it and its not working out. I saving the registered view in following ways and not working

mxd = arcpy.mapping.MapDocument("C:\GIS\testfile.mxd") 

Error: AssertionError: Invalid MXD filename.

------------------------------------------------------------

mxd = arcpy.mapping.MapDocument("CURRENT")


Error : RuntimeError: Object: CreateObject cannot open map document



0 Kudos
SithikSabithaAbdulkani
New Contributor
Thank you very much
0 Kudos
SethPaine1
New Contributor III

I get this error running from ArcMap 10.5 when when I insert my script tool into a ModelBuilder model.  Both the original script tool and the ModelBuilder tool are set to run in the foreground.  I do not get the error if I use the script tool outside of ModelBuilder.

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

Seth, I would recommend starting a new question post/thread.  INclude a few more details, the exact error and if possible, the python code in question following https://community.esri.com/people/curtvprice/blog/2014/09/25/posting-code-blocks-in-the-new-geonet?s...

also, you can tag other groups with the @ and the group name like @modelbuilder

krishmandali
New Contributor

I am writing a small automation to create a view, register the view in Geo database and publish it as a service. So far good with creating a view and register with Geo database but I am trying to save the mxd before publishing it and its not working out. I saving the registered view in following ways and not working

mxd = arcpy.mapping.MapDocument("C:\GIS\testfile.mxd") 

Error: AssertionError: Invalid MXD filename.

------------------------------------------------------------

mxd = arcpy.mapping.MapDocument("CURRENT")


Error : RuntimeError: Object: CreateObject cannot open map document
0 Kudos