I am trying to automate various tasks in ArcGIS Desktop (using ArcMap generally) with Python, and I keep needing a way to add a shapefile to the current map. (And then do stuff to it, but that's another story).

The best I can do so far is to add a layer file to the current map, using the following ("addLayer" is a layer file object):

def AddLayerFromLayerFile(addLayer):
 import arcpy
 mxd = arcpy.mapping.MapDocument("CURRENT")
 df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
 arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
 del mxd, df, addLayer

However, my raw data is always going be shapefiles, so I need to be able to open them. (Equivalently: convert a shapefile to a layer file without opening it, but I'd prefer not to do that).

2 Answers 2


Here's what I found worked:

import arcpy
from arcpy import env

# get the map document
mxd = arcpy.mapping.MapDocument("CURRENT")

# get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

# create a new layer
newlayer = arcpy.mapping.Layer(path_to_shapefile_or_feature_class)

# add the layer to the map at the bottom of the TOC in data frame 0
arcpy.mapping.AddLayer(df, newlayer,"BOTTOM")

The dataframe (variable df) that this code will put the new layer into is the first dataframe in the map document. Also note that this code adds the data as a new layer at the bottom of the TOC. You can also use the other arrangement options, which are "AUTO_ARRANGE" and "TOP".

  • 3
    Another option for specifying the dataframe is to use the active dataframe: df = mxd.activeDataFrame instead of df = arcpy.mapping.ListDataFrames(mxd)[0] -- also, you don't need the "*" in the listdataframe call.
    – jbalk
    Oct 20, 2017 at 7:02

Make Feature Layer (Data Management) http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000006p000000.htm

I just tried it in the Python window of ArcMap and it adds directly to my map ( I didn't have to get my dataframe and call AddLayer).

arcpy.MakeFeatureLayer_management('r:/temp/a.shp','test') alt text

  • That works great in the immediate window... but when I try the same code in a script file and run it, nothing happens! (The code runs without error messages, but nothing appears in the ToC) Also, if I remove the layer from the ToC, then try and run the code in the immediate window again, I get a "file already exists" type error. Where is the "test" layer file saved?
    – Tom W
    Jan 6, 2011 at 20:11
  • Did you install SP1? Jan 6, 2011 at 21:22
  • 1
    @Tom W: I'm pretty much a noob with python and arcpy. But I don't think a layer file is physically created. If you want a layer file, you have to make it and pass the layer name as a parameter: arcpy.SaveToLayerFile_management('test', 'r:/temp/evilmonkey.lyr', 'ABSOLUTE') If you don't want to save the layer and you just want it gone, arcpy.Delete_management('test'). Jan 6, 2011 at 21:55
  • @Tom W: just re-read your comment. Are you trying to add a layer to ArcMap from a separate python shell(not the immediate window)? I didn't think you could do that (but I'm no authority on this...maybe you can). Jan 6, 2011 at 22:02
  • 1
    @Tom W: I know about that layer bug, which is why I was making sure that SP1 was installed. To add the layer to the TOC as you're describing, you need your script to have a derived output feature layer and set the value to the name of the layer you made. GP tools in ArcMap try to protect the TOC from spurious layers (temp FCs etc) in GP tools so you need to define in the script tool's params that your new feature layer is going to stay in the TOC when done. Where the layer is living on disk is likely the data source of the FC, so a Feature Class named the same as the layer on your workspace. Jan 10, 2011 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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