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

6956
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
12 Replies
RichardFairhurst
MVP Honored Contributor

I just tested it to make sure nothing has changed.  A script run in Idle with the env.overwriteOutputs = True setting produces errors like this when attempting to overwrite an output with a tool.

Traceback (most recent call last):

  File "C:\Python26\ArcGIS10.0\temp_sums.py", line 26, in <module>

    arcpy.Statistics_analysis(intable, outtable, stats)

  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\analysis.py", line 1089, in Statistics

    raise e

ExecuteError: Failed to execute. Parameters are not valid.

ERROR 000725: Output Table: Dataset \\agency\agencydfs\CustomApps\rfairhur\Layers\Application.gdb\PARCELS_ASSESSOR_SUMS already exists.

Failed to execute (Statistics).

If the quote is true that env settings have no effect outside the script, then I have to consider this behavior a bug, since overwriteOutputs has never worked inside my scripts run from Idle.  However, this bug has existed since the old GP syntax and has never been addressed since python geoprocessing scripts were developed.

Based on Curtis' post it does seem that the overwrite outputs works as a tool.  However, most of my scripts are for after hours batch processing, so it does not help me in that case.  The Exists() and Delete_management() approach will work in any set up, so I recommend using it to avoid a gotcha if you want to do stand alone scripts.

0 Kudos
Zeke
by
Regular Contributor III

Hmm... not getting an error in a script I just wrote, but maybe it's not overwriting either? Will have to check this out. Good to know.

0 Kudos
MatthewHowe
New Contributor III

Instead of adding it in the main script, it works by adding it to the start of the validation script. In my case, a plot that I created with Matplotlib was being added to the TOC in the current ArcMap session. This prevented it from happening. If it isn't adding automatically, just switch the boolean to True.

import arcpy
class ToolValidator(object):
"""Class for validating a tool's parameter values and controlling
the behavior of the tool's dialog."""

arcpy.env.addOutputsToMap = False‍‍‍‍‍‍
0 Kudos