2

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() 
4
  • 1
    How skilled are you in Python? The script you posted above is that the exact script you used, or did you modify it in some way? How are you running the script? Are you just double clicking the .py file, or are you running it from the Python window? The script you posted contains 'Path of my toolbox'. Did you fill in the path to your toolbox? Because if you do not fill in your path, you will get an error.
    – LMB
    Jul 12, 2017 at 14:08
  • Yes I did give the path to my toolbox. I just have basic knowledge of python. I am running the python script from the python window available in ArcMap. Please help @LaurensBakker
    – PowerToYou
    Jul 12, 2017 at 14:14
  • How exactly did you specify the path? You should place it between " or ' if you run the script line for line, (so input one line, press enter twice, wait for the >>> to re-appear). At what line does the error occur? example gp.SetProduct("C:/folder1/subfolder2/file.extention")
    – LMB
    Jul 12, 2017 at 14:17
  • By the way, why would you want to run the model from python at all? Why not just from the arccatalog?
    – LMB
    Jul 12, 2017 at 14:26

2 Answers 2

1

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.

1
  • Hi this code worked for me. You were right I gave the path of the toolbox. Thank you @Hornbydd
    – PowerToYou
    Jul 12, 2017 at 17:28
0

You can export the model to python and paste the code into the script you found online:

http://desktop.arcgis.com/en/arcmap/10.3/analyze/modelbuilder/exporting-a-model-to-a-python-script.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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