ImportToolbox

Resumen

Imports a geoprocessing toolbox for use in ArcPy, allowing access to the toolbox's associated tools.

Nota:

This function is equivalent to the AddToolbox function.

Debate

While any of the core ArcGIS toolboxes are accessible by default in a script, your 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 various folders or geodatabases and may have many different origins; they may be toolboxes you have personally created, toolboxes created internally by your organization, or toolboxes you have downloaded from ArcGIS Online or other sites. In any case, these toolboxes must be imported into ArcPy before they can be used as tools in Python.

Server toolboxes can also be added using a semicolon delimiter.

ServerSyntax

Internet ArcGIS Server

URL;servicename;{username};{password}

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. Running ImportToolbox will then prompt the Sign In dialog box if not already signed in or will succeed if already signed in.

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

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

If a token has 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()

Sintaxis

ImportToolbox (input_file, {module_name})
ParámetroExplicaciónTipo de datos
input_file

The geoprocessing toolbox to be accessed from ArcPy.

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> or arcpy.<alias>.<toolname>). Since ArcPy depends on toolbox aliases to access and run the correct tool, aliases are important when importing custom toolboxes. It is recommended that you 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
Valor de retorno
Tipo de datosExplicación
Module

Returns the imported module.

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

Muestra de código

ImportToolbox example 1

Import geoprocessing toolbox for use in ArcPy.

import arcpy

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

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))
ImportToolbox example 1

Import a publicly available toolbox.

import arcpy

tbx = 'https://utility.arcgisonline.com/arcgis/rest/services/;' + \
      'Utilities/PrintingTools'
arcpy.ImportToolbox(tbx)
arcpy.PrintingTools.GetLayoutTemplatesInfo()

Temas relacionados