1

I have been looking at ways to run a ArcGIS model directly in arcpy, without having to export it and correct all sorts of things in the code. But somehow I can't seem to make it work. This is how I wrote the code, in which Workshop.tbx is the real name of the toolbox, TestAlias is the alias of the toolbox and Workshopmodel is the name of the model I want to run.:

import arcpy
arcpy.env.overwriteOutput = True

arcpy.env.workspace = r"c:/ArcGISdata"
arcpy.ImportToolbox(r"c:/ArcGISdata/Workshop.tbx")

arcpy.Workshopmodel_TestAlias

print "Script finished"

Can anyone help me along?

  • 1
    Generally, you'll receive better responses to questions if you can be more specific. Actual error messages, explanations of the behavior, both expected and what's actually happening, etc. – Evil Genius Apr 14 '16 at 11:26
4

You've only referenced the model, not called it.

In short, add () to the end of the call. So in your case: arcpy.Workshopmodel_TestAlias().

At a simple level, your model is a method, not a variable. However, this will only work if you're model doesn't have required parameters, or if all required parameters have default values (you specified values in the model and saved it). You'll have to also specify the parameters as well if they are required.

1

I finally managed to get it to work. And it is so simple, really:

import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"c:/ArcGISdata"  
arcpy.ImportToolbox(r'C:\ArcGISdata\Workshop.tbx')    
arcpy.Workshopmodel_TestAlias()   
print "Script finished"
0

try:

import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"c:/ArcGISdata"
arcpy.ImportToolbox(r"c:/ArcGISdata/Workshop.tbx")

#check if toolbox is imported...
print arcpy.ListToolboxes('Workshop')
#check if tool exist...
print arcpy.ListTools('Workshopmodel')

arcpy.Workshopmodel()
print "Script finished"
  • I copied into PyScripter, check syntax (was OK), but now I get SyntaxError: Invalid syntax. Doesn't that drive one crazy? – GIS10a Apr 14 '16 at 11:53
  • u'Workshop(TestAlias)'] [] I think we are almos there: Traceback (most recent call last): File "C:\ArcGISdata\Py\TestModule.py", line 11, in <module> arcpy.Workshopmodel() AttributeError: 'module' object has no attribute 'Workshopmodel'. – GIS10a Apr 14 '16 at 12:05
-1

For me, something is missing in your code. Something like the general.runalg() query should be in front of the instruction calling your model, basicly what it does is run your model after you've imported the arctoolbox. If i remember correctly you'll need to write it like this general.runalg(your model name, input data, output data)

  • 1
    I think you are referring to running a QGIS processing algorithm from Python. This particular question concerns ArcGIS and Arcpy. – Evil Genius Apr 14 '16 at 11:30
  • I also thought it needed a little more, but could not find it in the documentation. Since all the input parameters are already set in the model, I tried to run it with just general.runalg(Workshopmodel), but it won't recognize the command. – GIS10a Apr 14 '16 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.