[GIS] Calling a model from a python script

arcmaparcpymodelbuilder

I built a model using model builder which I want to run using a python script.

The error i get is:

AttributeError: Object: Tool or environment not
found

Can anyone please help me solve this as I am new to python.

The script I found online is:

import sys, string, os, arcgisscripting
import arcpy
arcpy.ImportToolbox
arcpy.env.workspace = 'Path of my geodatabase'
arcpy.env.overwriteOutput = True
# Create the Geoprocessor object...
gp = arcgisscripting.create() 

# Set the License...
gp.SetProduct("ArcView") 

# Load required toolboxes...
gp.AddToolbox("Path of my toolbox")

# Set the Overwrite property to True
gp.OverwriteOutput = 1 

# Run the model
# All arguments were supplied to model prior to saving
gp.MultiLine_Geocoder() 

Best Answer

You are mixing up the "pointers" to the geoprocessor object (arcpy then you create gp). You are clearly using some older code and consequently mixed up your code. Your code implies that the toolbox sits within a geodatabase so you should be using the correct path. This should work:

import arcpy
arcpy.ImportToolbox('Path of my geodatabase') # e.g. c:\temp\mygdb.gdb\myToolbox
arcpy.env.overwriteOutput = True
try:
    # Run the model
    # All arguments were supplied to model prior to saving
    arcpy.MultiLine_Geocoder() # ModelName_ToolboxAlias
except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

At this point I should point out there are always examples of how to call such functions in the help file! I suggest you get into the practice of looking at the code samples, so for calling models from within scripts you need to be reviewing the sample code for ImportToolbox. All tools\functions have code samples.

Related Question