[GIS] Creating Self Executing Python Script (*.py)

arcgis-desktoparcpypython

Trying to figure out how to create self executing python scripts with using the ArcGIS python window. I'm ok (generally) when developing and running scripts inside the python ArcGIS windows but unable to figure out self executing scripts.

For example ….here is a really simple script created to run a "Select Case" to change annotate attributes in one field (BBB) based upon attributes in field AAA. Goal is simply to double click the *.py file and have it run – outside of ArcGIS

# Import arcpy module
import arcpy

# Import env from arcpy as ENV and set the workspace environment
from arcpy import env
env.workspace = "C:/Users/Jeff Reichman/Documents/My GIS Projects/OpenStreetMap/OSM.gdb"

# Local variables:
Input_Field = "C:/Users/Jeff Reichman/Documents/My GIS Projects/OpenStreetMap/OSM.gdb/SaintLouis/SaintLouis_osm_ln"

# Process: Calculate Field
arcpy.CalculateField_management(Input_Field, "BBB", "x", "VB", "dim x as long/nselect case [AAA]/n  case 1: x = 100/n  case 2: x = 200/n  case 3: x = 300/n  case 4: x = 400/n  case 5: x = 500/n  case else: x = -999999/nend select")

Best Answer

I think your question is pure Python rather than GIS but I put together a simple Python script called test.py below to prove that it can be easily done.

import arcpy

arcpy.CreateFeatureclass_management("C:/temp","test.shp")

I placed test.py in C:\temp and used Windows Explorer to double-click it.

A DOS window appeared for about 10-15 seconds, and then closed. Half a second later Windows Explorer refreshed itself to show the expected shapefile (C:\temp\test.shp) had been created.

As @NathanW and @MichaelMiles-Stimson have commented, if this does not "just work":

  1. Check your file association for *.py
  2. Think about whether your Python script will find any required ArcGIS licensing available

This was running on a machine that has a Single Use license always available. Installing ArcGIS 10.2.2 for Desktop months ago installed Python and appears to have set the correct file association as part of that process.

Related Question