English

Error: AttributeError when calling a model or script tool through Python

Error Message

It is common to employ ModelBuilder to help automate a time-consuming process. The model may need to run afterhours as a scheduled task, and to do that, a Python script is needed:

import arcpy
arcpy.ImportToolbox(r"path\to\toolbox.tbx")
arcpy.ModelName_ToolboxAlias()
Usually, this executes without error. However, the code may raise an AttributeError such as:
'module' object has no attribute 'ModelName_ToolboxAlias'
or
Object: Tool or environment <> not found.
User-added image

User-added image

Cause

Calling a model through Python may raise an AttributeError because of one of the following:

  • the model does not exist
  • the model name is spelled incorrectly
  • the toolbox alias was spelled incorrectly
  • there may be spaces or underscores, or
  • the model was created in ArcGIS Pro and is called with the ArcPy installed with ArcGIS for Desktop
To troubleshoot the exact cause, try:
import arcpy
arcpy.ImportToolbox(r"path\to\toolbox.tbx")
arcpy.ToolboxAlias.ModelName()
The error differs slightly, which helps to find the exact object Python cannot find.
  1. ​AttributeError: 'module' object has no attribute 'Model'
    This indicates that the model does not exist, the model name is spelled incorrectly, or there may be spaces or underscores.
  2. AttributeError: 'module' object has no attribute 'ToolboxAlias'
    This indicates that the toolbox alias was spelled incorrectly, or there may be spaces or underscores.
Note:
Models that are newly created, as well as any models that have been edited and saved in ModelBuilder in ArcGIS Pro, cannot be used in other ArcGIS for Desktop applications. Therefore, attempts to call a model created in ArcGIS Pro with the ArcPy in Python 2.7, will raise an AttributeError. See: ModelBuilder: Migration to ArcGIS Pro

Solution or Workaround

The solutions are to:

  • ensure the model exists
  • ensure that the toolbox alias and model name are spelled correctly
  • remove spaces or underscores in the names and aliases
  • use the Python installed in ArcGIS Pro to call models created in ArcGIS Pro
For example, in the Toolbox Properties dialog, under Alias, remove the underscore ( _ )

User-added image User-added image

The underscore ( _ ) can also be removed from the model or script tool Properties dialog, if necessary:

User-added image User-added image
Once the underscores ( _ ) are removed from the toolbox alias and/or the tool name, the tool can be used in the Python window.
User-added image

Related Information