ArcPy script runs in Python window but not as a tool

4938
4
Jump to solution
05-22-2015 09:46 PM
CameronPallotta
New Contributor

I'm trying to write a simple selection tool which works just fine in the Python window in ArcMap but when I create a tool with it, it says that it runs successfully but nothing happens. The script is designed to create a layer file called locations_lyr from a file geodatabase feature class called locations. It then selects entities where Facility_Type = School and refreshes the ArcMap display. The script is as follows:

import arcpy
import os
from arcpy import env
#set workspace
Workspace = arcpy.GetParameterAsText(0)
env.workspace = r"D:\GIS Data\Selection Test\Temp_OP.gdb"
env.overwriteOutput = 1
arcpy.MakeFeatureLayer_management("Locations","locations_lyr")
arcpy.SelectLayerByAttribute_management(in_layer_or_view="locations_lyr",selection_type="NEW_SELECTION",where_clause="Facility_Type = 'School'")
arcpy.RefreshActiveView()

If I load it in the Python console, it works just fine. I've added it as a tool called SelectSchools in a new toolbox called Selection using the Add Script wizard and that all seems to work just fine. When I run the tool, it says it ran successfully but nothing happens. I've tried adding Print statements at various points in the script but they are not displayed. Any suggestions would be appreciated!

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

"locations_lyr" is a new layer you are creating in the script. Your script is creating it in memory, but is not adding it to the map.

If you want it added to the map  table of contents, you need to:

1. Go into the script tool properties parameters tab, and set up a derived parameter of type Feature Layer.

2. Edit the script to set the parameter value

arcpy.SetParameterAsText(0, "locations_lyr")

No RefreshActiveView should be necessary, the layer will appear the map (if you run the script tool "in process").

UPDATE: For the output to be added to the map Add Outputs To Map must be on. (In ArcMap this is found in Geoprocessing > geoprocessing settings. From the Python window, this is an environment setting.

Using geoprocessing options to control tool execution—Help | ArcGIS for Desktop

You can also turn this on and off in the geoprocessing enviroinment setting addOuyputsToMap

env—Help | ArcGIS for Desktop

View solution in original post

4 Replies
XanderBakker
Esri Esteemed Contributor

Actually the arcpy.RefreshActiveView() will only have effect if the code has access to the Current dataframe. If the code is run as stand alone script you will not be able to use it. You will have to use the mapping module for this.

curtvprice
MVP Esteemed Contributor

"locations_lyr" is a new layer you are creating in the script. Your script is creating it in memory, but is not adding it to the map.

If you want it added to the map  table of contents, you need to:

1. Go into the script tool properties parameters tab, and set up a derived parameter of type Feature Layer.

2. Edit the script to set the parameter value

arcpy.SetParameterAsText(0, "locations_lyr")

No RefreshActiveView should be necessary, the layer will appear the map (if you run the script tool "in process").

UPDATE: For the output to be added to the map Add Outputs To Map must be on. (In ArcMap this is found in Geoprocessing > geoprocessing settings. From the Python window, this is an environment setting.

Using geoprocessing options to control tool execution—Help | ArcGIS for Desktop

You can also turn this on and off in the geoprocessing enviroinment setting addOuyputsToMap

env—Help | ArcGIS for Desktop

BradJones
New Contributor III

Where would you enter this in the script?

0 Kudos
DarrenWiens2
MVP Honored Contributor

You can enter this any time after you've created the feature layer called "locations_lyr".