Select to view content in your preferred language

Importing a Python Toolbox (ArcGIS Pro 2.9)

1428
5
04-21-2022 04:02 PM
BlakeTerhune
MVP Regular Contributor

I'm not sure what I'm doing wrong with arcpy.ImportToolbox() in ArcGIS Pro 2.9. No matter what I do, it always errors with

OSError: The toolbox file C:\temp\MyToolbox.pyt was not found.

There's not much to it besides calling arcpy.ImportToolbox(). What am I missing?

0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

Does it have an alias?

ImportToolbox—ArcGIS Pro | Documentation

If the toolbox does not have an alias, the module_name is required.

See the example

What is a Python toolbox?—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
BlakeTerhune
MVP Regular Contributor

Thanks Dan. It does have an alias.

While double checking, I decided to check something else and I think I found the problem. I'm importing separate .py files into my Python toolbox (like the Spatial Analyst Supplemental Tools). I took that part out and it seems to import fine.

Ultimately what I'm trying to do is have a Python toolbox with some basic utility functions used by many tools. Each tool is saved as a separate .py file and imported into the Python toolbox. I would like to be able to call functions defined in the Python Toolbox from the separate, imported .py scripts. Testing  arcpy.ImportToolbox() was just the first step.

0 Kudos
BlakeTerhune
MVP Regular Contributor

And to clarify further, I currently have the utility functions in their own .py file that is imported into each individual tool's .py file. This is working, but I'd like it to be in the Python toolbox .pyt file so I can encrypt it.

0 Kudos
DanPatterson
MVP Esteemed Contributor

I don't have that problem with "custom" toolboxes.  It may be a solution.  Although, I tend to have my own *.py file imported by the toolbox, which may contain esri tools, rather than importing them directly


... sort of retired...
0 Kudos
by Anonymous User
Not applicable

Here's sample code that worked in ArcMap, but I have not tested in Pro.

# standard python toolbox formatting for your utility toolbox
# C:/path/to/this/toolbox.pyt
class Toolbox(object):
    def __init__(self):
        self.label = 'your label'
        self.alias = 'some_alias'
        self.tools = [tool1, tool2, tool3]

# later on when you want to call them, e.g. from another .pyt
ImportToolbox(input_file='C:/path/to/this/toolbox.pyt')
arcpy.some_alias.tool1(arg1, arg2, ...)
arcpy.some_alias.tool2(arg1, ...)
arcpy.some_alias.tool3(arg1, ...)
0 Kudos