How to import a python toolbox?

16682
15
Jump to solution
11-01-2013 06:54 AM
ionarawilson1
Occasional Contributor III
Can anybody tell me how to import a python toolbox and access the parameter of a tool inside the toolbox?

I have started importing the toolbox using the ImportToolbox function but I am not sure if this is correct:

    def onSelChange(self, selection):          ptoolbox = arcpy.ImportToolbox("D:/ArcGISData/SARS/Python_10April2013/Toolbox.pyt")
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III
Say you have a tool called MyTool in C:\Users\me\my.pyt and you've given the toolbox the alias mynewtools.

arcpy.ImportToolbox(r"C:\Users\me\my.pyt") arcpy.mynewtools.MyTool("a", "b", "c")


This is all very clearly documented in the help.

View solution in original post

15 Replies
JasonScheirer
Occasional Contributor III
That is how you import a toolbox. What do you mean "access the parameters"? What are you trying to do?
0 Kudos
ionarawilson1
Occasional Contributor III
I would like to change the values of a tool parameter. How can I do that?
0 Kudos
JasonScheirer
Occasional Contributor III
You can't. The Python toolbox objects are isolated from your Python interpreter.
0 Kudos
ionarawilson1
Occasional Contributor III
So, there is no way  to access a tool parameter if not inside a toolbox? What can I access when I import the toolbox?
0 Kudos
AdamMesser1
New Contributor III

Python toolboxes should not be considered isolated from your data management and analysis workflow. Many of us have a desire to use the functionality in our python toolboxes in conjunction with our other arcpy code. You could build an easy to import companion .py file to hold your toolbox business logic next to your actual toolbox  (ie. myTbx.pyt and myTbxFunctions.py), however, pyt files can be accessed and used in your interpreter or IDE by using imp. The example below shows this with our metadataUpdateItems tool. There are a couple important things to note if using pyt outside of ArcCatalog:

  • python toolbox parameters have a valueAsText attribute that is set when running via ArcCatalog and will not be created when building the arcpy.parameter object in this manner.
    • The alternative to valueAsText is to interpret the .value attribute as a string where needed
  • Additional arcpy.parameter attributes are updated in ArcCatalog including, but not limited to, param.altered or param.hasBeenValidated, in the case below we added a variable to bypass those checks when not using in arcCatalog

import imp

def getParamByName(params, name):
    outParam = None
    for param in params:
        if param.name == name:
            outParam = param

#import toolbox
myTbx = imp.load_source('t',r'C:\dsCode\toolboxes\myTbx.pyt')
#build the toolbox parameter objects list
params = myTbx.metadataUpdateItems().getParameterInfo()
#retrieve the parameter to update and change its value
inLyr = getParamByName(params, 'inLyr')
inLyr.value = 'C:\\temp\mySDElyr'
#update the parameters
#Note: the second argument is a runAsFunction binary T/F
# this is to bypass the parameter.altered or parameter.validated checks of the updateParameters code
myTbx.metadataUpdateItems().updateParameters(params,True)
#update a list parameter
agolTags = getParamByName(params, 'agolTags') 
agolTags.values = [u'wildlife (open data)']
#execute the toolbox
myTbx.metadataUpdateItems().execute(params, None)
JasonScheirer
Occasional Contributor III
When you import the toolbox you can call the tools.
0 Kudos
ionarawilson1
Occasional Contributor III
How? Can you show me an example? Thanks
0 Kudos
JasonScheirer
Occasional Contributor III
Say you have a tool called MyTool in C:\Users\me\my.pyt and you've given the toolbox the alias mynewtools.

arcpy.ImportToolbox(r"C:\Users\me\my.pyt") arcpy.mynewtools.MyTool("a", "b", "c")


This is all very clearly documented in the help.
TedChapin
Occasional Contributor III

Nowhere in the documentation for ImportToolbox does it say to use arcpy.alias.toolname().  It says to use arcpy.toolname_alias().  Your syntax works, the syntax in the documentation does not, which is what led me here.  Thanks for the tip, however you discovered it.

Even using the latest documentation:

ImportToolbox—Help | ArcGIS for Desktop