<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Run model builder model from Python with variable for model name in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/run-model-builder-model-from-python-with-variable/m-p/28597#M2189</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Freddie, it's not necessary 'stitch together the call to the tool along with its variables' for the eval call. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i.e.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;print eval('int(1.23)')&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;i.e.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;a = int
b = eval('int')
print int(1.23), a(2.34), b(3.45)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;1 2 3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 21:09:05 GMT</pubDate>
    <dc:creator>Luke_Pinner</dc:creator>
    <dc:date>2021-12-10T21:09:05Z</dc:date>
    <item>
      <title>Run model builder model from Python with variable for model name</title>
      <link>https://community.esri.com/t5/python-questions/run-model-builder-model-from-python-with-variable/m-p/28593#M2185</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am writing a Python script to run a model builder model based off of variables.&amp;nbsp; 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'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I use the actual model name (arcpy.StreetNameListingModel_TBX()) it works but I want the model name determined by a variable.&amp;nbsp; Is it possible?&amp;nbsp; I have tried with quotes in different places, combining the needed text before using, but none of it works.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;CODE&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;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()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:08:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/run-model-builder-model-from-python-with-variable/m-p/28593#M2185</guid>
      <dc:creator>David_van_Riel</dc:creator>
      <dc:date>2021-12-10T21:08:57Z</dc:date>
    </item>
    <item>
      <title>Re: Run model builder model from Python with variable for model name</title>
      <link>https://community.esri.com/t5/python-questions/run-model-builder-model-from-python-with-variable/m-p/28594#M2186</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I edited the code to provide syntax highlighting.&lt;/P&gt;&lt;P&gt;try adding a line before # 13 to emulate&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; a = r"c:\path\with spaces in it"
&amp;gt;&amp;gt;&amp;gt; tbx = "toolboxname"
&amp;gt;&amp;gt;&amp;gt; ext = ".tbx"
&amp;gt;&amp;gt;&amp;gt; toolbox = a + "\\" + tbx + ext
&amp;gt;&amp;gt;&amp;gt; toolbox
'c:\\path\\with spaces in it\\toolboxname.tbx'&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:09:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/run-model-builder-model-from-python-with-variable/m-p/28594#M2186</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-10T21:09:00Z</dc:date>
    </item>
    <item>
      <title>Re: Run model builder model from Python with variable for model name</title>
      <link>https://community.esri.com/t5/python-questions/run-model-builder-model-from-python-with-variable/m-p/28595#M2187</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can however use the &lt;A href="https://docs.python.org/2/library/functions.html#evalhttp://" rel="nofollow noopener noreferrer" target="_blank"&gt;eval()&lt;/A&gt;​ function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;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')&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:09:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/run-model-builder-model-from-python-with-variable/m-p/28595#M2187</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-12-10T21:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: Run model builder model from Python with variable for model name</title>
      <link>https://community.esri.com/t5/python-questions/run-model-builder-model-from-python-with-variable/m-p/28596#M2188</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 Oct 2015 21:57:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/run-model-builder-model-from-python-with-variable/m-p/28596#M2188</guid>
      <dc:creator>FreddieGibson</dc:creator>
      <dc:date>2015-10-22T21:57:02Z</dc:date>
    </item>
    <item>
      <title>Re: Run model builder model from Python with variable for model name</title>
      <link>https://community.esri.com/t5/python-questions/run-model-builder-model-from-python-with-variable/m-p/28597#M2189</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Freddie, it's not necessary 'stitch together the call to the tool along with its variables' for the eval call. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i.e.&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;print eval('int(1.23)')&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;i.e.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;a = int
b = eval('int')
print int(1.23), a(2.34), b(3.45)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;1 2 3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:09:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/run-model-builder-model-from-python-with-variable/m-p/28597#M2189</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-12-10T21:09:05Z</dc:date>
    </item>
  </channel>
</rss>

