Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to automate various tasks in ArcGIS Desktop (using ArcMap generally) with Python, and I keep needing a way to add a shape file 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 shape files, so I need to be able to open them. (Equivantly: convert a shape file to a layer file without opening it, but I'd prefer not to do that).

share|improve this question

2 Answers 2

up vote 12 down vote accepted

So, I wrestled with this exact same problem for quite a while. Here's what I found to work:

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 a number of other arrangement options such as "AUTO_ARRANGE".

share|improve this answer
Perfect! (Note to other newbies who come across this: remember to add "import arcpy" at start) –  Tom W Jul 25 '11 at 13:45
Hello all, maybe this is a little off-topic but I got the exact same problem with tables. I got to produce a tableview in a script, to run some other linear referencing modules and I cannot add the table to the TOC. Thanks in advance, Annalisa –  Annalisa Minelli Mar 1 '13 at 10:11

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

share|improve this answer
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 '11 at 20:11
Did you install SP1? –  Jason Scheirer Jan 6 '11 at 21:22
@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'). –  Jay Cummins Jan 6 '11 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). –  Jay Cummins Jan 6 '11 at 22:02
@Jay It seems like the file doesn't exist on yoru hard drive, yet exists somewhere, otherwise Arc wouldn't complain it already exists! I'm trying to add a shape from a script run from within ArcMap. (Eventually, the script will do various things with the shape file after its added - it's not just there to open the file!). –  Tom W Jan 7 '11 at 14:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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