Wednesday, May 4, 2011

Arcpy Envrionmental Variables in Python

Esri's arcpy library has a common set of parameters that it uses among all it's tools.  These parameters are originally obtained from the geoprocessing environment that all tools use during execution.  When a tool is launched, the script/model/tool goes out and grab the general environmental settings. 

From the help:
"When a script is run inside a tool from an ArcGIS application or from another geoprocessing script, the environment settings used by the calling application or script are passed to it. These settings become the default settings used by the tool's script when it is executed. The called script may alter the settings passed to it, but those changes are only used within that script or by any other tool it may call."
The environmental settings are exposed as properties on the env class.  These properties can be used to retrieve the default values and to set custom values. 

Setting Workspace Example:

>>> import arcpy
>>> from arcpy import env
>>> env.workspace = "c:/data"

To get a python list of all envrionmental values do the following:

>>> import arcpy
>>> print arcpy.ListEnvironments()

You can load and save envrionmental settings to a file using the

>>> # Load settings
>>> arcpy.LoadSettings(file)
>>> # Save Settings
>>> arcpy.SaveSettings(saveFile)


Enjoy