Select to view content in your preferred language

Making a Python Toolbox - Need Help

729
5
06-26-2023 01:57 PM
EvanMyers1
Frequent Contributor

I have been trying to get a python toolbox working but no matter what I do, it always comes back with the red exclamation point "Click to repair project item".

I have several functions in the .pyt code, and at the bottom I have the typical "if __name__ == '__main__':" at the bottom.  Is this wrong?  My script is several hundred lines long so here is a shorted version:

import arcpy
import sys
import os

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Toolbox"
        self.alias = "toolbox"

        # List of tool classes associated with this toolbox
        self.tools = [Tool]


class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = False


    def setup(sqlQuery):
        print("ice cream")
    def numberLS(lsOIDList, lsNoWW_NUM):
        print("banana")
    def numberManholes(mhOIDList, mhNoMH_NUM):
        print("chocolate syrup")
    def numberGravityMains(mhOIDList, grvmainOIDList, grvmainNoID)
        print("peanuts")

    if __name__ == '__main__':

        # Global Environment settings
        arcpy.env.overwriteOutput = True

        aprx = arcpy.mp.ArcGISProject('current')
        currentMap = aprx.activeMap
        lyrList = currentMap.listLayers()

        sqlQuery = "OWNEDBY = 1 AND LIFECYCLESTATUS = 'ACTIVE'"
        setup(sqlQuery)
        numberLS(lsOIDList, lsNoWW_NUM)
        numberManholes(mhOIDList, mhNoMH_NUM)
0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

dedent lines 33- onward


... sort of retired...
EvanMyers1
Frequent Contributor

Thanks for the reply.  I Fixed the indentation issue but now I am getting 'setup is not defined' when I check the syntax.  I looked at examples and they all included the 'self' variables so I added those (I don't know what its for), and now its saying 'NameError: name 'self' is not defined'.

All I'm trying to do is get my script to run in a Python Toolbox so I can reference it in a C# script (another headache).  Is any of this class, self, stuff needed?  Seems much more complex than a normal toolbox.

0 Kudos
Luke_Pinner
MVP Regular Contributor

That won't work.  Python Toolboxes aren't scripts. They are libraries and as such don't really get run, they get imported, the Tool class is instantiated and the tools execute method is called. You are missing all the required Tool methods (execute, getParameterInfo, isLicensed etc...)

I think what you need is a simple script, either standalone or attached to a regular toolbox.

curtvprice
MVP Esteemed Contributor

Best way to start is to create the .pyt from the ArcGIS Pro catalog window which will give you a complete framework stub ready to go with all the required pieces.

0 Kudos
EvanMyers1
Frequent Contributor

Thanks for the help everyone.  I have decided to switch gears and use a stand alone script and import it.

0 Kudos