Running an Arcpy stand alone script on PyScripter I followed everything in this answer to ArcPy gives error Object: CreateObject cannot open map document?


  1. Start up the Streets.mxd in ArcMap
  2. Run the mxd=arcpy.mapping.MapDocument("CURRENT") inside the ArcMap (without no error)

enter image description here
3- Add the same mxd=arcpy.mapping.MapDocument("CURRENT") to my code in PyScripter IDE as

import arcpy
####mxd = arcpy.mapping.MapDocument(r"C:\Temp\Street.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
targetGroupLayer = arcpy.mapping.ListLayers(mxd, "Main", df)[0]
addLayer = arcpy.mapping.Layer(r"C:\Data\NetMap.gdb\Streets\OneWay")
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM")‍‍‍‍‍‍

but I am getting this runtime error message:

enter image description here

Traceback (most recent call last): File "C:\Users\bhosseini\Documents\ArcPy\Trace With Graphics\module3.py", line 3, in mxd=arcpy.mapping.MapDocument("CURRENT") File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\arcobjects\mixins.py", line 625, in init super(MapDocumentMethods, self).init(mxd) File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\arcobjects_base.py", line 47, in init for arg in args)) RuntimeError: Object: CreateObject cannot open map document

  • 1
    The python script using MapDocument("CURRENT") must be run from the currently open MXD. You can't use CURRENT from a stand-alone script because there's no MXD for it to use - it's running in a separate process so isn't aware of the one you already have open. – Midavalo May 19 '17 at 17:24
  • 1
    Please see gis.stackexchange.com/questions/61158/… – Midavalo May 19 '17 at 17:59
  • You said that you have "followed everything in this answer to gis.stackexchange.com/questions/21906" but that says that what you are trying to do cannot be done and what you are seeing is the expected software behaviour. – PolyGeo Jun 1 '17 at 8:40

Browse other questions tagged or ask your own question.