def onSelChange(self, selection): ptoolbox = arcpy.ImportToolbox("D:/ArcGISData/SARS/Python_10April2013/Toolbox.pyt")
Solved! Go to Solution.
arcpy.ImportToolbox(r"C:\Users\me\my.pyt") arcpy.mynewtools.MyTool("a", "b", "c")
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:
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)
arcpy.ImportToolbox(r"C:\Users\me\my.pyt") arcpy.mynewtools.MyTool("a", "b", "c")
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: