1

I have a custom made Toolbox and I can't make it run.

The Toolbox is called toolbox_k and the tool is called sn

I did this:

arcpy.ImportToolbox(r"C:\Users\user\Desktop\VST\Toolbox_k.tbx", "sn")

I use the sn with the parameters as using the GUI version and it doesn't work. It gives a syntax error:

sn('pathone','pathtwo') 

UPDATE

Running:

>>> arcpy.ImportToolbox("Toolbox_PS.tbx", "tbxk")

gives:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\__init__.py", line 125, in ImportToolbox
    return import_toolbox(input_file, module_name)
  File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\toolbox_code.py", line 457, in import_toolbox
    mymodule = generate_toolbox_module(toolbox, None, False, False, False, module_name, use_alt_alias)
  File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\toolbox_code.py", line 432, in generate_toolbox_module
    'exec')
  File "C:\Users\user\Desktop\VST\Toolbox_k.tbx", line 19
    def 00CalculateKAEK(PST=None):
         ^
SyntaxError: invalid syntax
4
  • The documentation states the expected naming, which is in the arcpy namespace: arcpy.toolbox_tool
    – Vince
    Commented Aug 30, 2018 at 10:32
  • So what should be changed? Commented Aug 30, 2018 at 11:40
  • When you post saying you have an error, please include the actual error text.
    – user2856
    Commented Aug 30, 2018 at 12:10
  • It was actually as you read it. Simply Syntax error. Commented Aug 30, 2018 at 12:14

1 Answer 1

1

From the documentation:

ImportToolbox (input_file, {module_name})
  • input_file - The geoprocessing toolbox to be accessed from Python.
  • module_name (optional) - 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._ or arcpy..). 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.

So try:

# *If* you have set an alias in your .tbx properties, 
# assuming you set the alias as "tbxk"
arcpy.ImportToolbox(r"C:\Users\user\Desktop\VST\Toolbox_k.tbx")

# If you haven't set an alias, use a temporary alias "tbxk"
arcpy.ImportToolbox(r"C:\Users\user\Desktop\VST\Toolbox_k.tbx", "tbxk")

# then
arcpy.sn_tbxk(etc...)
# or
arcpy.tbxk.sn(etc...)

Re. your update with theSyntaxError - python names can contain numbers, but must start with a letter or an underscore so you'll have to rename def 00CalculateKAEK(PST=None)

4
  • And then use the parameters in the last part as working with a function? Commented Aug 30, 2018 at 12:53
  • Generally yes. Assuming you have specified those parameters in your tool properties.
    – user2856
    Commented Aug 30, 2018 at 12:57
  • When importing the toolbox as you suggested with an alias it still gives a runtime error. See updated question with the error Commented Aug 30, 2018 at 13:35
  • Your function name def 00etc.... Functions, variables and other names can not start with a number, they must start with a letter (or an underscore which indicates a non public name by convention).
    – user2856
    Commented Aug 30, 2018 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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