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)



Chrysanthi Polyzoni said...

Unfortunately when i load my environment settings either as an .xml file or through a python script, they do not seem to work out. In order to succesfully run my custom tool, I need to manualy specify Environment Variables through the Environment settings grachic user interface dialog box.

I work with ArcGIS 10.1 and I need to specify Workspace,geographicTransformations, outputCoordinateSystem, mask, cellSize, extent and snapRaster environment variables before I run my script.

Is there any idea to fix this isue?

Thank you

M313R said...

Hi Chrysanthi,

I'm new to using python in arcgis and I'm having the same problem. Have you figured out a solution? I'm also running ArcGIS 10.1


Andrew said...

There is a bug for this issue: Bug NIM-102490.

You could use read the XML file manually and set the parameters that way, or just use a configuration file and set the environmental parameters that way.