Why doesn't Python script add output automatically to map?

6818
12
Jump to solution
11-24-2014 09:39 AM
KirstynAlex
New Contributor II

This is the first Python script I've written in a few years, so this is probably a rookie question.

I wrote this script that creates 3 different buffers around a point shapefile, and it works, but doesn't automatically add the buffers to the map, it just saves them in the specified workspace. In my Geoprocessinng options I have it set to add outputs to map and it works with all ESRI tools but not my script. Any ideas? Thanks in advance!

Here is my script:

# Script Name: EpicenterBuffers
# Description: Creates 10, 50, and 100 KM
#              buffers around an epicenter
# Created By:  Kirstyn Pittman
# Date:        11/24/14


import arcpy
arcpy.env.workspace = r"H:\Drafting Department\Python\EpicenterBuffers"
shp = "Location"
distanceList = ["10 kilometers","50 kilometers","100 kilometers"]
# Loop through each distance in the distanceList
for dist in distanceList:
    outName = shp+"_"+dist
    arcpy.Buffer_analysis(shp, outName, dist)
print "Finished Buffering"
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor

A standalone python script is not a geoprocessing tool, and the import of arcpy does not replicate all of the behaviors that operate within an open session of ArcMap.  Environment settings that apply directly to an open session of ArcMap are ignored by Python scripts, such as arcpy.overwriteOutputs = True and arcpy.addOutputsToMap = True; but they are exposed through arcpy to control the user experience in an open session of ArcMap (i.e., whatever arcpy did to these settings will persist and affect how ArcMap responds when the user tries to save over a file or run a geoprocessing tool, so a programmer can control user capabilities).

You need to explicitly do these tasks with arcpy.  You have to use the arcpy.mapping.AddLayer method to have your script add layers after creating new data.  You also cannot overwrite data, and have to test if it exists and then delete it with arcpy.Exists() and arcpy.Delete_management() (the Exist sample code demonstrates this).

View solution in original post

12 Replies
TimothyHales
Esri Notable Contributor

Try:

arcpy.addOutputsToMap = true

env(arcpy)

0 Kudos
KirstynAlex
New Contributor II

Thanks! I tried that, gives me this error though:

Executing: EpicenterBuffer C:\GISData\10345_01_VineyardPointe\LOCATION.shp

Start Time: Mon Nov 24 09:56:05 2014

Running script EpicenterBuffer...

<type 'exceptions.NameError'>: name 'true' is not defined

Failed to execute (EpicenterBuffer).

Failed at Mon Nov 24 09:56:06 2014 (Elapsed Time: 1.00 seconds)

0 Kudos
TimothyHales
Esri Notable Contributor

It accepts a Boolean value, so that should have been capitalized.  0/1 or True/False

0 Kudos
KirstynAlex
New Contributor II

Oh I see! Well I didn't get an error that time and the tool still worked like it did before--still doesn't add the outputs to ArcMap but adds them to my workspace. Not sure what the problem is!

0 Kudos
RichardFairhurst
MVP Honored Contributor

A standalone python script is not a geoprocessing tool, and the import of arcpy does not replicate all of the behaviors that operate within an open session of ArcMap.  Environment settings that apply directly to an open session of ArcMap are ignored by Python scripts, such as arcpy.overwriteOutputs = True and arcpy.addOutputsToMap = True; but they are exposed through arcpy to control the user experience in an open session of ArcMap (i.e., whatever arcpy did to these settings will persist and affect how ArcMap responds when the user tries to save over a file or run a geoprocessing tool, so a programmer can control user capabilities).

You need to explicitly do these tasks with arcpy.  You have to use the arcpy.mapping.AddLayer method to have your script add layers after creating new data.  You also cannot overwrite data, and have to test if it exists and then delete it with arcpy.Exists() and arcpy.Delete_management() (the Exist sample code demonstrates this).

KirstynAlex
New Contributor II

Thank you, I understand!

0 Kudos
curtvprice
MVP Esteemed Contributor

If you set up the python script as a script tool with parameters for your output feature classes they will be added to your map - when the script is run as a script tool from ArcMap. If you are generating the pathnames inside the script, the output parameters would be set up as derived parmeters and you would return their values using the arcpy SetParameterAsText method.

This is all covered in the help on script tools.

Zeke
by
Regular Contributor III

Wait - are you saying that if I set arcpy.overwriteOutput = True in a stand alone script run from an IDE (PyScripter, in my case) it has no effect? Or maybe I'm misunderstanding. That's not my takeaway from the help files, 'Environment values set within scripts only apply to the execution of the script'.‌ Am I missing something?

0 Kudos
RichardFairhurst
MVP Honored Contributor

Yes, I am saying that based on years of having that environment parameter ignored.  I left it in my scripts, but always get errors that I am trying to overwrite existing files if that is the only parameter used to overwrite files in my script.  It was only the Exist() method and Delete_management() methods that have allowed me to use Scheduler or Idle for my scripts.  I don't think that option works at all outside of the Python window inside a live session of ArcMap or perhaps a tool run within an open ArcMap session.  Python has no innate interactions with ArcMap, and importing arcpy does not set your script up with full a Desktop like environment without interacting with an actual ArcMap session.

0 Kudos