Error when running arcpy.mapping code as toolbox script

Discussion created by soatley on Feb 2, 2011
Latest reply on Mar 29, 2017 by rastrauch


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]

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\", line 3, in <module>
mxd = arcpy.mapping.MapDocument("CURRENT")
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\ArcPy\arcpy\arcobjects\", line 441, in __init__
super(MapDocumentMethods, self).__init__(mxd)
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\ArcPy\arcpy\arcobjects\", 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".