Run ArcPy tool dynamically in python

5699
8
10-06-2014 06:44 AM

Run ArcPy tool dynamically in python

Did you ever wanted to run a tool in Python dynamically? Without "hard coding" the tool name and the parameters in a script?

This is the normal, static syntax to call a tool:

result = arcpy.Buffer_management("input", "output", 100)

If you need to run a tool depending on a condition, and you want to make the name and the parameters dynamic, you can use this syntax:

tool = "Buffer_management"

arguments = ["input", "output", 100]

result = getattr(arcpy, tool)(*arguments)

So the built-in gettattr-method calls the method tool with arguments as parameter values, the asterisk is used to unpack the list of arguments for the call of the tool.

Tags (1)
Comments

A very interesting way of running a tool! I would not claim to be a python expert so probably showing my ignorance but I was wondering under what scenario would you ever run a tool in this manner? I've never seen this technique used in any of the ESRI help documentation.

Thanks for the tip.

Yes it is very interesting, I tried and it works, only thing is I am using 10.2.2 and "Buffer_management" needs to be replaced with "Buffer_analysis".

That is pretty nifty. It took me a moment to realize it's dynamic in that the code can dynamically choose the tool to run (based on some logic) rather than getting dynamic input from the user.

And when you say unpack the arguments, you're referring to the items in the list. If your tool only has one argument, I found you can pass it just bare (without the list) if you get rid of the asterisk. But that is just me experimenting, your way is recommended since it will accommodate all cases.

Thanks for the post!

A potential use might be to decide which tool to use on the fly (maybe while iterating through a geodatabase with arcpy.da.walk) depending on if you're looking at a table or a feature class. Like make a feature layer or a table view. Still don't know what particular use case would require that. I'm sure someone's needed this though!

In order to decide what tool to use on-the-fly, you would have to have either an if..elif...else structure, dictionary map, or some other control structure.  In that case, why not call the tool like normal.  The other hang up is arguments because different tools have different arguments.  Granted, some tools share syntax/arguments, but not enough to make one argument list to rule them all.

From a learning perspective, and understanding more nuts and bolts of Python, code snippets like this are useful even if the real-world need is limited.

Can you provide an example where this could be useful? I came up with the following (maybe you don't know if you want to 'AddXY_management' or 'RepairGeometry_management' or any other one-parameter tool), but am having a hard time thinking how you could write much code beyond a simple tool call that applies to multiple tools:

import arcpy

tool = arcpy.GetParameterAsText(0)

input = arcpy.GetParameterAsText(1)

arguments = [input]

result = getattr(arcpy, tool)(*arguments)

Stephan, I can't run this because I am on an iThingy and only have python installed... but is this the type of thing you are suggesting could be done?  TOTALLY untested

This would be useful for repetitive tasks for tools that share a common set of inputs etc and for things like backup after a process.  Very nice, since it complements using the Results window to get students to learn how to script

EDIT

Posting a blog on this right now after I have had some sleep.  here is the URL

Darren...just scraping the possibilities "Hey did you know this?" Tool Results on steroids...

Version history
Last update:
‎10-06-2014 06:44 AM
Updated by:
Contributors