My script for schedule a model run fails ???

503
6
03-13-2012 04:52 AM
HenrikSvenningsen
Occasional Contributor
My script to call a model:

import sys, os, arcpy

arcpy.ImportToolbox(r"x:\MB_Svenne\AutoRun.tbx", "TBX")

arcpy.Test_TBX()

The answer from IDLE:

Traceback (most recent call last):
  File "X:/MB_Svenne/AUTORUN1", line 5, in <module>
    arcpy.Test_TBX()
AttributeError: 'module' object has no attribute 'Test_TBX'

PLEASE HELP i havn't a clue :confused:
6 Replies
DuncanHornby
MVP Frequent Contributor
Henrik,

According to Help:

The best practice is to assign an alias when first creating the toolbox rather than using ImportToolbox to assign a temporary one that will only be applicable in Python.


So give your toolbox an alias and drop the "TBX" from:

arcpy.ImportToolbox(r"x:\MB_Svenne\AutoRun.tbx", "TBX")


Duncan
0 Kudos
HenrikSvenningsen
Occasional Contributor
Hi Duncan

Thx, but this is not my favorit area, so i'm still a bit lost :)

I gave my toolbox an alias as "AUTO" and my script looks like this now (the model i want to call is named "Test" and is working as a stand alone model):

import sys, os, arcpy

arcpy.ImportToolbox(r"x:\MB_Svenne\AutoRun.tbx")

arcpy.Test_AUTO()

But the result in IDLE is still the same ?
0 Kudos
DuncanHornby
MVP Frequent Contributor
Henrik,

Are you getting the same error message? Unless it's top secret could you zip up your model and upload it?

Just to make sure I understand you have a model in a toolbox and you are trying to execute it from IDLE with the code you have posted on this thread?

Duncan
0 Kudos
HenrikSvenningsen
Occasional Contributor
Hi Duncan

I think it may be my model and due to that there is Parametre in it. I just made a simple "Clip Model" and it works.

Basicly i want.

  • Schedule Windows 7 to run a py script

  • The py script to start a model

  • Model to run in night time.

  • Result to be ready when i meet in to work


I should not be ana "hokus pokus" with the script from above in my first mail, or ?
0 Kudos
DuncanHornby
MVP Frequent Contributor
Henrik,

If your model takes a parameter (e.g. a polygon FeatureClass that is the clip polygon for a clip tool) and this will never change then you could unset this parameter as it never changes. So your model is a model without parameters. This is how your original python script was calling the model

# Calls a model called Test (with no parameters) in a toolbox with the Alias set to AUTO
arcpy.Test_AUTO()


If your model has an input exposed as an input parameter then you will need to supply this. It's important that paramerers are supplied in the order as they are defined in the model properties (parameters tab).

# Calls a model called Test (with 1 parameter) in a toolbox with the Alias set to AUTO
arcpy.Test_AUTO(r"c:\temp\clipPoly.shp") 


You should not assume your model will always run without error so you should make your code smarter by looking at the result object the model returns and deal with an error accordingly

resObj = arcpy.Test_AUTO(r"c:\temp\clipPoly.shp")
if resObj.status == 4:
    print "success!"
else:
    print "failure :(" 
0 Kudos
HenrikSvenningsen
Occasional Contributor
Thanks a lot Duncan ;) it's running now.
0 Kudos