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"
Solved! Go to Solution.
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).
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)
It accepts a Boolean value, so that should have been capitalized. 0/1 or True/False
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!
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).
Thank you, I understand!
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.
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?
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.