0

This question has been asked multiple times with proper answers:

but I still cannot get it right.

I am using ArcGIS 10.2.2.

I want to add via a Python standalone script a Featureclass from a file-gdb to the TOC. If I try it in the ArcGIS Python window it works fine. However in a standalone script nothing happens. There is no error message or anything. I already thought it might be a bug, but it seems to work for everyone else. I also tried it with a .lyr and it didn´t work either. Someone got a hint for me?

Here is the code (I also tried the one from the ArcGIS help site, and from all the links above).

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\GIS\KUP\Check.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
addLayer = arcpy.mapping.Layer(r"C:\GIS\KUP\Ergebnisse.gdb\Potenziale_singlepart_final")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

I also tried :

addLayer = arcpy.mapping.Layer(r"C:\GIS\KUP\Potenziale_singlepart_final.lyr")

What am I doing wrong? I am trying for a week now. I`m getting crazy with this one. Seems so easy.

10
  • 3
    You need to save the mxd afterward to make the changes permanent. Use mxd.save() or mxd.saveACopy (file_name, {version}) ... see resources.arcgis.com/en/help/main/10.1/index.html#//… Commented Sep 9, 2015 at 23:40
  • @MichaelMiles-Stimson by "nothing happens" I think Sebastian is saying that the layer isn't added to the map at all (not that the change isn't permanent)? Commented Sep 10, 2015 at 0:03
  • yes thats right
    – Sebastian
    Commented Sep 10, 2015 at 0:07
  • 2
    As you're creating a mapdocument object from a static (on disc) map and not "Current" the changes are not permanent, when you open the map document it appears that nothing has happened because the changes are discarded as soon as the mapdocument goes out of scope - at least that's my understanding of how that object works. Try saving it and see if the layer is added. The code shown is sound, it should work, so unless there's some code that isn't shown that is problematic the only logical conclusion is that the changes are made but not committed. Commented Sep 10, 2015 at 0:15
  • 1
    You cannot use "CURRENT" in a standalone script, only when you have a map open in ArcMap. If that works for you then I think you are doing something slightly different to what you have presented in your code snippet and question description.
    – PolyGeo
    Commented Sep 10, 2015 at 1:15

1 Answer 1

2

On the code snippet and symptoms presented in the question:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\GIS\KUP\Check.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
addLayer = arcpy.mapping.Layer(r"C:\GIS\KUP\Ergebnisse.gdb\Potenziale_singlepart_final")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

it seems to me, like @MichaelMilesStimson has commented, that you are missing the line of code at the end which is necessary to persist any changes made to the MXD you open as a Map Document object:

mxd.save()

or, if you prefer:

mxd.saveACopy (file_name, {version}) 

Not the answer you're looking for? Browse other questions tagged or ask your own question.