I'm working through the recipes in the Python cookbook, and am having some weird results.

When I run the script (copied below) with

  • the map reference listed as "CURRENT" from an open mxd

  • from the Python window in Arcmap

I get the expected results -- a layer is added to the map.

However, when I:

  • change the map reference to a path
  • save the script as a .py, and
  • run it from idle

no errors are thrown, but no layer is added. I even tried nesting the whole thing in a try/except/else statement, and I got back the else response.

I know the mxd path is correct, because I copied it from ArcCatalog.

Any ideas?

# Import arcpy.mapping module
import arcpy.mapping as mapping

# Create a map reference
mxd = mapping.MapDocument(r"H:\Python\Python_Cookbook\Maps\TravisCounty.mxd")

# Create a reference to the primary data frame where the layer will be added
df = mapping.ListDataFrames(mxd)[0]

# Define the reference layer
refLayer = mapping.ListLayers(mxd, "Building_Permits*", df)[0]

# Define the layer to beinserted relative to the reference layer:
insertLayer = mapping.Layer(r"H:\Python\Python_Cookbook\data\TravisCounty\School_District_2.lyr")

# Insert the layer into the df
  • A little cleanup on what you have used that worked vs what did not, would be helpful. As well as a little clarification on the question title would possibly get you more hits, as the current title is not descriptive.
    – MaryBeth
    Jul 21, 2015 at 16:40
  • My bad - my first post here! Is there a way to edit the title to include more detail?
    – Raquel
    Jul 21, 2015 at 18:01

2 Answers 2


Try saving the mxd using the save() method or just refresthing the display/TOC.

If scripting is used to modify the appearance of some map document elements while using the CURRENT map document (for example, change a layer name, the data frame extent, and so on), the map may not automatically update with each executed line of code. To refresh the map document to reflect the changes, use either the RefreshActiveView or RefreshTOC functions. These functions will refresh the map display or page layout and table of contents, respectively. The refresh functions are only needed if you want to see the application updated. Arcpy.mapping export, save, and printing functions will generate the expected updated results without using these functions.

Documentation here.

  • I did try this, but it didn't help, unfortunately. Also, when the script ran successfully from the Python window, the new layer just popped right in, without needing to be refreshed or saved.
    – Raquel
    Jul 21, 2015 at 17:49
  • @Raquel as well as calling mxd.save(), make sure you don't have the mxd open in ArcMap when you run the script in idle.
    – user2856
    Jul 22, 2015 at 11:15

Try using the absolute path in Python. Instead of H:, what does H: point to? For example:

mxd = mapping.MapDocument(r"\FileServer\ThisIsWhereHPoints\Python\Python_Cookbook\Maps\TravisCounty.mxd")

More about path referencing here.

  • 1
    Also, you can check if your path exists to ArcPy using the arcpy.Exists() function. These types of checks help narrow down the possible problem.
    – Branco
    Jul 21, 2015 at 16:59
  • Well, I went ahead and changed the path to an absolute path, and then used arcpy.Exist() to verify that it really existed (True). Still no new layer, unfortunately...
    – Raquel
    Jul 21, 2015 at 17:50

Your Answer

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

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