Select to view content in your preferred language

Not Able to Add FC from GDB Dataset to an MXD using ArcPy Stand Alone Script

679
4
Jump to solution
10-19-2018 10:50 AM
BruceGreen
New Contributor III

I am trying to add a Feature Class from a Geodatabase Dataset to an MXD and save a copy like this but I am getting following errors on each methos

import arcpy
import os

arcpy.env.workspace = r"C:\arcgis\ArcTutor\AAA\MXDs"
MXD = arcpy.mapping.MapDocument(r"C:\arcgis\ArcTutor\AAA\MXDs\map.mxd")
DF = arcpy.mapping.ListDataFrames(MXD)[0]

lyr = arcpy.mapping.Layer("C:\arcgis\ArcTutor\AAA\GDBs\Delaware.gdb\Kent\Kent")
arcpy.mapping.AddLayer(DF, layer, "AUTO_ARRANGE")
MXD.saveACopy('newmxd.mxd')
del MXD
print(" Done")‍‍‍‍‍‍‍‍‍‍‍‍

enter image description here

and

import arcpy
import os
arcpy.env.workspace = r"C:\arcgis\ArcTutor\AAA\MXDs"
MXD = arcpy.mapping.MapDocument(r"C:\arcgis\ArcTutor\AAA\MXDs\map.mxd")
DF = arcpy.mapping.ListDataFrames(MXD)[0]

lyr = "C:\arcgis\ArcTutor\AAA\GDBs\Delaware.gdb\Kent\Kent"

arcpy.MakeFeatureLayer_management(lyr, 'shplyr')
arcpy.mapping.AddLayer(DF, lyr, "AUTO_ARRANGE")
print(" Step 4")
MXD.saveACopy('newmxd.mxd')
del MXD
print(" Done")

enter image description here

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Try:

import arcpy
import os
arcpy.env.workspace = r"C:\\arcgis\\ArcTutor\\AAA\\MXDs"
MXD = arcpy.mapping.MapDocument(r"C:\\arcgis\\ArcTutor\\AAA\\MXDs\\map.mxd")
DF = arcpy.mapping.ListDataFrames(MXD)[0]
fc = "C:\\arcgis\\ArcTutor\\AAA\\GDBs\\Delaware.gdb\\Kent\\Kent"
lyr = arcpy.MakeFeatureLayer_management(fc, 'shplyr')[0]
arcpy.mapping.AddLayer(DF, lyr, "AUTO_ARRANGE")
print(" Step 4")
MXD.saveACopy('newmxd.mxd')
del MXD
print(" Done")

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

you were doing so well on the raw encoding, then you stopped

"C:\arcgis\ArcTutor\AAA\GDBs\Delaware.gdb\Kent\Kent"

'C:\x07rcgis\\ArcTutor\\AAA\\GDBs\\Delaware.gdb\\Kent\\Kent'

notice what \a produces   \x07rcgis

raw encode, double backslashes or single forward slashes

BruceGreen
New Contributor III

Hi Thank and Thanks for reply I changed the code to 

import arcpy
import os
arcpy.env.workspace = r"C:\\arcgis\\ArcTutor\\AAA\\MXDs"
MXD = arcpy.mapping.MapDocument(r"C:\\arcgis\\ArcTutor\\AAA\\MXDs\\map.mxd")
DF = arcpy.mapping.ListDataFrames(MXD)[0]
lyr = "C:\\arcgis\\ArcTutor\\AAA\\GDBs\\Delaware.gdb\\Kent\\Kent"
arcpy.MakeFeatureLayer_management(lyr, 'shplyr')
arcpy.mapping.AddLayer(DF, lyr, "AUTO_ARRANGE")
print(" Step 4")
MXD.saveACopy('newmxd.mxd')
del MXD
print(" Done")‍‍‍‍‍‍‍‍‍‍‍‍

and now I am getting  AssertionError:  on  assert isinstance(add_layer, Layer)

File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\mapping.py", line 49, in AddLayer
 assert isinstance(add_layer, Layer)
AssertionError
0 Kudos
DanPatterson_Retired
MVP Emeritus

line 6 isn't a layer as in *.lyr 

line 7 you make a feature layer.. form lyr, and you call it 'shplyr'... did you try that in line 8? since line 6 won't work.

also be prepared that AddLayer with become addLayer in ArcGIS Pro... subtle differences

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Try:

import arcpy
import os
arcpy.env.workspace = r"C:\\arcgis\\ArcTutor\\AAA\\MXDs"
MXD = arcpy.mapping.MapDocument(r"C:\\arcgis\\ArcTutor\\AAA\\MXDs\\map.mxd")
DF = arcpy.mapping.ListDataFrames(MXD)[0]
fc = "C:\\arcgis\\ArcTutor\\AAA\\GDBs\\Delaware.gdb\\Kent\\Kent"
lyr = arcpy.MakeFeatureLayer_management(fc, 'shplyr')[0]
arcpy.mapping.AddLayer(DF, lyr, "AUTO_ARRANGE")
print(" Step 4")
MXD.saveACopy('newmxd.mxd')
del MXD
print(" Done")