Select to view content in your preferred language

Python Toolbox "Error checking python syntax" making debugging difficult; trying to import tool scripts.

189
1
Jump to solution
06-11-2024 10:21 AM
Labels (1)
Glasnoct
New Contributor III

The code within my toolbox.pyt file runs without complaint within the python console, and I can tell its importing my module as expected (since it just prints a "hello" when imported) but the same code causes the toolbox in the catalog to throw up red eclamation mark and the "check syntax" option only returns "Error checking python syntax" instead of giving me a traceback so I can't tell what's going wrong in there. I'm planning to make this toolbox easy to send to coworkers so the toolbox code is set to find the toolbox file's location and add it to path, then import tool scripts from the same directory or subdirectories. Any idea what it's complaining about?

 

# -*- coding: utf-8 -*-
# https://pro.arcgis.com/en/pro-app/latest/arcpy/classes/parameter.htm

import arcpy
from pathlib import Path
import sys
import importlib

# find the toolbox location and add it to PATH
toolbox_name = 'Arcpy Script Repo'
for entry in arcpy.mp.ArcGISProject('current').toolboxes:
    if toolbox_name in entry['toolboxPath']:
        main_script_dir = str(Path(entry['toolboxPath']).resolve().parent)
        sys.path.append(main_script_dir) # assumption hierarchy will stay as base folder/subfolder/script.py for this and all other scripts
        break

# add script subdirectories to PATH
sys.path.append(main_script_dir + '\\QC')
# import all scripts and their respective tool classes
import qc_check_unconnected_cables_and_conduits
importlib.reload(qc_check_unconnected_cables_and_conduits)
from qc_check_unconnected_cables_and_conduits import QC_Check_Unconnected_Cables_And_Conduits


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

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

 

 

0 Kudos
1 Solution

Accepted Solutions
Glasnoct
New Contributor III

Found the issue just by chance while referencing this github of a python toolbox example. Way down at the bottom is a comment that offhandedly mentions " #exit(0) # This causes the toolbox not to load in ArcGIS Pro. Whatever."

 

Want to guess what was at the end of my imported script? exit()!

Toolbox reports no issues now. 😁

View solution in original post

0 Kudos
1 Reply
Glasnoct
New Contributor III

Found the issue just by chance while referencing this github of a python toolbox example. Way down at the bottom is a comment that offhandedly mentions " #exit(0) # This causes the toolbox not to load in ArcGIS Pro. Whatever."

 

Want to guess what was at the end of my imported script? exit()!

Toolbox reports no issues now. 😁

0 Kudos