Running an Arcpy stand alone script on PyScripter
I followed everything in this answer to ArcPy gives error Object: CreateObject cannot open map document?
As:
- Start up the
Streets.mxd
in ArcMap
- Run the
mxd=arcpy.mapping.MapDocument("CURRENT")
inside the ArcMap (without no error)
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")
mxd=arcpy.mapping.MapDocument("CURRENT")
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")
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
but I am getting this runtime error message:
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
MapDocument("CURRENT")
must be run from the currently open MXD. You can't useCURRENT
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