Error: AttributeError when calling a model or script tool through Python
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.
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
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.
- 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.
- 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
The underscore ( _ ) can also be removed from the model or script tool Properties dialog, if necessary:
Once the underscores ( _ ) are removed from the toolbox alias and/or the tool name, the tool can be used in the Python window.
- Using a geoprocessing service in Python scripts
- ModelBuilder: migration to ArcGIS Pro
- Python Documentation 3.4.5: Built-in Exceptions