How does one specify the Environments honored by a geoprocessing tool in a Python toolbox and have them show up in the Environments tab?

1069
20
02-22-2018 06:39 AM
JacquesBeaurain
New Contributor III

Example

This is for inside ArcGIS Pro but I assume it also applies to Desktop. I assume there is some way to advertise the environments in same way as the internal tools do but could not locate a single example or mention of it in the documentaion.

Thanks

20 Replies
DanPatterson_Retired
MVP Esteemed Contributor

I set them and/or derive them from input parameters.

http://pro.arcgis.com/en/pro-app/tool-reference/environment-settings/current-workspace.htm begins a whole list.

If I want to set something I do it via code, so as to never leave anything to chance or by assumption.

Is there something specific you are wondering about? the arcpy.da.Describe returns lots more information in dictionary form 

For example...

pth = "drive:\...some...\...path...\to_a.gdb"

desc = arcpy.da.Describe(pth)
desc.keys()

dict_keys(['catalogPath', 'FIDSet', 'baseName', 'children', 'childrenExpanded',
 'connectionProperties', 'connectionString', 'currentRelease', 'dataElementType',
 'dataType', 'domains', 'extension', 'file', 'fullPropsRetrieved', 'metadataRetrieved',
 'name', 'path', 'release', 'workspaceFactoryProgID', 'workspaceType'])
0 Kudos
JacquesBeaurain
New Contributor III

Thanks, but that is not quite what I am looking for. I know how to get the parameters from the arcpy.env variable and do so to do things like build pyramids for generated rasters if that is the current env setting. I could duplicate all the inputs that is normally in the environments tab as inputs for my tool (with defaults being the env variable setting), but that seems needlessly cumbersome. Most of the ESRI tools that honor environment settings advertise them in the second tab. I am looking to do the same e.g.:

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

That is what I was saying, I do it by code.  The only way you 'access' what is set is by what is set at the project level. 

Arcpy can help... you can access some of these through.... arcpy's GetSystemEnvironment ... ListEnvironments plus others in that area.

As you know, at the end of each tool's help there is the list of the Environments that tool observes.  If I want to ensure a specific setting, I set it.  If you are creating script tools for arctoolbox, then there are environments that can be set there

http://pro.arcgis.com/en/pro-app/help/analysis/geoprocessing/basics/create-a-python-script-tool.htm

Environment

A parameter can derive its default value from a geoprocessing environment. So if the specified geoprocessing environment is set, the value will be used as the default for the given parameter when the tool's dialog box opens.

If using tools and/or models, there is a hierarchy that is followed as discussed here

http://pro.arcgis.com/en/pro-app/tool-reference/environment-settings/what-is-a-geoprocessing-environ...

So setting things at the project level is your first course of action, then at the tool level.  If you don't set anything at the tool level then whatever you did or didn't set gets passed on to the tool.  Hence, my preference for setting things via code since it is easy to find what environment parameters are honored by any existing tool.

0 Kudos
JacquesBeaurain
New Contributor III

And that is what I said. I know how to do it by code and can do so, and I am not setting anything I am getting the project environment settings.

To maybe make make it clearer, I am implementing my own custom Python toolbox (.pyt file mechanism) and want to have an environments tab populated with the controls for each environment that it respects. 

What I want is to advertise it in the same way as the built in tools so that the Environment tab is not empty and I do not need to needlessly duplicate every one of the parameters as inputs to my tool so that user's can both see what the tool honors and can override it if they want (e.g. pick a different output coordinate system). Less magic than the tool just using these settings in the code and replicates the experience for the built in tools.

It is very possible that this is simply not possible currently in a Python toolbox. Was just wondering if I am missing something.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Ahhhh Now I get you.  Here is a custom tool that I haven't completed setting the environments for

Looks like they aren't empty but the default when you go to each is the project (aka system) level option first.

If I want it to 'show up' in there, I set it.

Set them in tool validation script is one option... setting directly in the tool's script is my preferred option...

saving and loading (LoadSettings.... from an xml stored in the same folder as the script is a 3rd option.

They are your control options.

JacquesBeaurain
New Contributor III

Thanks. I will see if I can somehow load settings saved from a Custom toolbox but not sure this will work. Note I am not implementing a Custom toolbox but rather a Python toolbox. In the Python toolbox that tab is empty.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

A tip...

import sys

script = sys.argv[0]  # ---- for eample

script
'C:/Git_Dan/arraytools/tools.py'

xml = 'my.xml'        # ---- stored in the same location as your script

pth = script.split("/")[:-1]  # or "\\" for split

pth.append(xml)

pth                   # ----- all the bits... and everything is relative to the script
['C:', 'Git_Dan', 'arraytools', 'my.xml']

xml_path = "\\".join(pth)

xml_path

'C:\\Git_Dan\\arraytools\\my.xml'
XanderBakker
Esri Esteemed Contributor

... or use the os module:

import sys
import os

xml = 'my.xml'
xml_path = os.path.join(os.path.split(sys.argv[0])[0], xml)
0 Kudos
JacquesBeaurain
New Contributor III

Oh this won't work. I am interested in respecting the current settings and not loading preset constant values from an XML file. Even for custom tools one cannot seem to hide the environments that doe not apply and Using the Environments field is not really useful:

0 Kudos