ImportToolbox (arcpy)

Summary

Imports the specified toolbox into ArcPy, allowing for access to the toolbox's associated tools.

Discussion

While any of the core ArcGIS toolboxes are accessible by default in a script, your own custom or third-party toolboxes must be added using ImportToolbox to use them in a script.

Other toolboxes may be found in any number of different folders or geodatabases, and may have many different origins; they may be toolboxes you have personally created, or are toolboxes created internally by your organization, or are toolboxes you have downloaded from sites like the Geoprocessing Resource Center. In any case, these toolboxes need to be imported into ArcPy in a one-step process before they can be used as tools in Python.

Server toolboxes can also be added using a semicolon delimiter.

Server

Syntax

Internet ArcGIS for Server

URL;servicename;{username};{password}

Learn more about using a geoprocessing service in Python

Syntax

ImportToolbox (input_file, {module_name})
ParameterExplanationData Type
input_file

The geoprocessing toolbox to be added to the ArcPy site package.

String
module_name

If the toolbox does not have an alias, the module_name is required.

When a tool is accessed through the ArcPy site package, the toolbox alias where the tool is contained is a required suffix (arcpy.<toolname>_<alias>). Since ArcPy depends on toolbox aliases to access and execute the correct tool, aliases are extremely important when importing custom toolboxes. A good practice is to always define a custom toolbox's alias. However, if the toolbox alias is not defined, a temporary alias can be set as the second parameter.

String
Return Value
Data TypeExplanation
Module

Returns the imported module.

If needed, tool names can be accessed from the module's __all__ property.

Code Sample

ImportToolbox example

Import geoprocessing toolbox for use in ArcPy.

import arcpy

# Import custom toolbox
arcpy.ImportToolbox("c:/tools/My_Analysis_Tools.tbx")

try:
    # Run tool in the custom toolbox.  The tool is identified by
    #  the tool name and the toolbox alias.
    arcpy.GetPoints_myanalysis("c:/data/forest.shp")
except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

Related Topics

6/21/2013