ArcGIS Desktop

  • ArcGIS Pro
  • ArcMap

  • My Profile
  • Help
  • Sign Out
ArcGIS Desktop

ArcGIS Online

The mapping platform for your organization

ArcGIS Desktop

A complete professional GIS

ArcGIS Enterprise

GIS in your enterprise

ArcGIS for Developers

Tools to build location-aware apps

ArcGIS Solutions

Free template maps and apps for your industry

ArcGIS Marketplace

Get apps and data for your organization

  • Documentation
  • Support
Esri
  • Sign In
user
  • My Profile
  • Sign Out

Help

  • Home
  • Get Started
  • Map
  • Analyze
  • Manage Data
  • Tools
  • More...

ImportToolbox

  • Summary
  • Discussion
  • Syntax
  • Code sample

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 toolboxes you have downloaded from sites such as 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.

ServerSyntax

Internet ArcGIS for Server

URL;servicename;{username};{password}

Learn more about using a geoprocessing service in Python

ImportToolbox also supports working with secured ArcGIS online services using an ArcGIS Server connection (.ags) file. Using an .ags file allows credentials to be stored within the file, hidden from sight.

import arcpy
arcpy.ImportToolbox('c:/logistics/logistics.ags;World/ServiceAreas')
arcpy.GenerateServiceAreas_ServiceAreas()

To allow any arcgis.com user to access your tool, use the UseSSOIdentityIfPortalOwned keyword. Executing ImportToolbox will then prompt the Sign In dialog box if not already signed in, or will just succeed if already signed in.

import arcpy
tbx = "http://logistics.arcgis.com/arcgis/services;World/ServiceAreas;UseSSOIdentityIfPortalOwned"
arcpy.ImportToolbox(tbx)
arcpy.GenerateServiceAreas_ServiceAreas()
Note:

The Sign In dialog box will be used, running a stand-alone Python file.

If a token has already been previously obtained, it can also be used with ImportToolbox.

import arcpy

token = 'sadsa213d2j32jsdw02dm2'
referrer = 'http://www.arcgis.com/'
tbx = 'http://logistics.arcgis.com/arcgis/services;' + \
      'World/ServiceAreas;token={};{}'.format(token, referrer)
arcpy.ImportToolbox(tbx)
result = arcpy.GenerateServiceAreas_ServiceAreas()

Syntax

ImportToolbox (input_file, {module_name})
ParameterExplanationData Type
input_file

The geoprocessing toolbox 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

  • CreateGISServerConnectionFile
  • Using tools in Python
  • Adding toolboxes in Python

ArcGIS Desktop

  • Home
  • Documentation
  • Support

ArcGIS Platform

  • ArcGIS Online
  • ArcGIS Desktop
  • ArcGIS Enterprise
  • ArcGIS for Developers
  • ArcGIS Solutions
  • ArcGIS Marketplace

About Esri

  • About Us
  • Careers
  • Esri Blog
  • User Conference
  • Developer Summit
Esri
Tell us what you think.
Copyright © 2019 Esri. | Privacy | Legal