Run model builder model from Python with variable for model name

2767
4
Jump to solution
10-22-2015 01:58 PM
David_van_Riel
New Contributor III

I am writing a Python script to run a model builder model based off of variables.  I can get all the way to calling the model itself but then I get the error message: unsupported operand type(s) for +: 'builtin_function_or_method' and 'str'.

When I use the actual model name (arcpy.StreetNameListingModel_TBX()) it works but I want the model name determined by a variable.  Is it possible?  I have tried with quotes in different places, combining the needed text before using, but none of it works.

CODE

import arcpy

##Set folder location of the Toolbox.
ToolboxFolder = r"C:\GIS\Street name listing"

##Set the name of the Toolbox.
ToolboxName = "Street_Name_Listing"

##Set the model name.
ModelName = "StreetNameListingModel"

##Call the Toolbox.
arcpy.ImportToolbox(ToolboxFolder+"\\"+ToolboxName+".tbx", "TBX")

##Call the model.
arcpy.ModelName+_TBX()
0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

I had a quick look at the ArcGIS help and there doesn't seem to be an arcpy way of calling a tool/model with a name from a variable.

You can however use the eval()​ function.

import os
import arcpy 

##Set folder location of the Toolbox. 
ToolboxFolder = r"C:\Temp" 

##Set the name of the Toolbox. 
ToolboxName = "Toolbox" 

##Set the model name. 
ModelName = "Model" 

##Call the Toolbox. 
arcpy.ImportToolbox(os.path.join(ToolboxFolder, ToolboxName+".tbx"), "TBX") 

##Call the model. 
model = eval("arcpy.{}_TBX".format(ModelName))
result=model('blah blah')

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

I edited the code to provide syntax highlighting.

try adding a line before # 13 to emulate

>>> a = r"c:\path\with spaces in it"
>>> tbx = "toolboxname"
>>> ext = ".tbx"
>>> toolbox = a + "\\" + tbx + ext
>>> toolbox
'c:\\path\\with spaces in it\\toolboxname.tbx'
Luke_Pinner
MVP Regular Contributor

I had a quick look at the ArcGIS help and there doesn't seem to be an arcpy way of calling a tool/model with a name from a variable.

You can however use the eval()​ function.

import os
import arcpy 

##Set folder location of the Toolbox. 
ToolboxFolder = r"C:\Temp" 

##Set the name of the Toolbox. 
ToolboxName = "Toolbox" 

##Set the model name. 
ModelName = "Model" 

##Call the Toolbox. 
arcpy.ImportToolbox(os.path.join(ToolboxFolder, ToolboxName+".tbx"), "TBX") 

##Call the model. 
model = eval("arcpy.{}_TBX".format(ModelName))
result=model('blah blah')
FreddieGibson
Occasional Contributor III

I agree with Luke on this one. If you're trying to call the name of a tool on the fly you'll want to stitch together the call to the tool along with its variables so that you can execute an eval call. Otherwise you could use one script to literally write another script that calls the tool and execute the newly created script. This would equivalent to asking how to call a function in python on the fly without knowing the name of the function in advance.

Luke_Pinner
MVP Regular Contributor

Freddie, it's not necessary 'stitch together the call to the tool along with its variables' for the eval call.

i.e.

print eval('int(1.23)')

You only need to use the eval call to get a reference to the model/tool, then you just treat the model object as a normal python function and call it as such.

i.e.

a = int
b = eval('int')
print int(1.23), a(2.34), b(3.45)

1 2 3