3

I have a tool I made with a python script called 'myTool' in a toolbox called 'myBox', alias 'box'. I need to run this tool multiple times with the output of one run being one of the inputs of the next run. I would like to write a python script that would run the tool a few times. I have a script called myScript.py:

import arcpy

myTool_box('input_1.shp', 'base_case.shp', 'output_1.shp') # creates output_1.shp
myTool_box('input_2.shp', 'output_1.shp', 'output_2.shp') # creates output_2.shp
myTool_box('input_3.shp', 'output_2.shp', 'final_output.shp') # creates final_output.shp

When I run execfile(myScript.py) from the python window, I get the error:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Projects\myScript.py", line 3, in <module>
    arcpy.myTool_box('input_1.shp', 'base_case.shp', 'output_1.shp') # creates output_1.shp
AttributeError: 'module' object has no attribute 'myTool_box'

I assume this means that my_tool or my custom toolbox isn't on arcpy's path or I need to import the toolbox to the script, but I haven't found any information on how to tell the script where the tool resides. What am I missing here?

  • Have you created your own toolbox that your script is run from in ArcToolbox? You should be able to specify an alias for this toolbox, and then when you run your tool you reference the alias and the tool to run it. You can also point arcpy directly at your toolbox if necessary. – Midavalo Apr 7 '17 at 14:43
  • I tried to reference the toolbox alias by adding _box after the tool, but it still couldn't find it. How do you point arcpy at the toolbox? that's probably the easiest option? – David Grover Apr 7 '17 at 15:22
2

Take a look at Adding toolboxes in Python and ImportToolbox

Import the toolbox, giving it an alias (myToolbox), then run your tool, referencing the tool name NameOfTool and the alias myToolBox separated by an underscore -> arcpy.NameOfTool_myToolbox()

arcpy.ImportToolbox(r"C:\Projects\MyToolbox.tbx", "myToolbox")

arcpy.NameOfTool_myToolbox('input_1.shp', 'base_case.shp', 'output_1.shp') # creates output_1.shp
arcpy.NameOfTool_myToolbox('input_2.shp', 'output_1.shp', 'output_2.shp') # creates output_2.shp
arcpy.NameOfTool_myToolbox('input_3.shp', 'output_2.shp', 'final_output.shp') # creates final_output.shp
  • ImportToolbox was what I was looking for. Not sure how that never came up in my google searches. – David Grover Apr 7 '17 at 15:44
  • Is this answer still current? It just did not work for me in Esri desktop 10.3 (Python 2.7). The underscore and toolbox name caused an error, instead I just typed arcpy.NameOfTool which worked. – Dylan Warburg Dec 4 '18 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.