Select to view content in your preferred language

Python Toolbox error handling: "Error checking python syntax" is too vague

872
6
Jump to solution
11-14-2024 08:12 AM
Glasnoct
Regular Contributor

If my python toolbox fails to open about half the time it will provide a traceback in the result window after clicking "check syntax" and any other time it simply gives me "error checking python syntax" which doesn't provide any info to go off of. Is there a way to guarantee a traceback message so I can get a sense of where it's breaking? Enclosing the main function that runs at the end of the toolbox code in a try/except and trying to print or AddMessage the resulting error produces nothing; I'm not sure what I need to do to get anything to show up in that resulting text box. The error is coming somewhere after the .pyt file calls the imported function (which has its own try/except error handling) but those error messages never appear.

 

Glasnoct_0-1731600768857.png

 

0 Kudos
1 Solution

Accepted Solutions
Glasnoct
Regular Contributor

Alright, I figured out a fix/workaround so in lieu of getting an answer as to how to print messages to the python error popup window...

Typically my errors/warnings/notifications are coded as the following so wherever I happen to run the code, I'll always see the messages

 def error(msg: str, abort=False):
        arcpy.AddError(msg)  # so it shows up in the GP tool messages
        print(msg)  # so it shows up in console/notebook
        if abort:
            sys.exit()  # immediately terminates if desired

 

Since it appears neither AddMessage nor print() work when checking the syntax, instead you can just raise an Exception with a custom message string instead. This will terminate execution though (which in this specific case you wouldn't mind) but at least you can catch a proper error now.

try:
    do_something()
except:  # shouldn't use a bare except but whatever
    raise Exception('something went horribly wrong')
    # execution terminates and the python error popup window displays the exception message

View solution in original post

6 Replies
AlfredBaldenweck
MVP Regular Contributor

Normally it gives a lot better message than that, which is suspicious haha.

Try using something like Pycharm; it'll give you the number of syntax errors 

AlfredBaldenweck_0-1731601242482.png

and if you click on that warning, it'll tell you exactly the issue(s).

AlfredBaldenweck_1-1731601280870.png

 

 

0 Kudos
Glasnoct
Regular Contributor

There are no syntax errors in Pycharm for the files; this is breaking somewhere in the execution within ArcPro itself. It works on my end but I'm troubleshooting why I can't get it to work on my coworkers' project and I know it has something to do with how its trying to handle information it retrieves from the map and map metadata as those are the only variables I have to change to make it break. I have error handling/messages for when it tries to validate those particulars but again, those don't show up in the python error resulting popup so I can't tell the where/why of the issue.

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Solved: Python Toolbox "Error checking python syntax" maki... - Esri Community

 

Maybe this might help?

Super weird that it's not working for your coworker. Does it work in a different project of yours, too? Or just the one you test in?

0 Kudos
Glasnoct
Regular Contributor

That's my last thread on the same topic but my solution isn't for the problem I'm having now. What I'm beginning to think is that the error handling is making this difficult because anywhere it is/should be breaking returns a print/addmessage and then calls sys.exit() but those messages do not appear or are overridden by the "error checking python syntax" message instead. Is there a way to print things to the Python Errors pop up or no?

0 Kudos
Glasnoct
Regular Contributor

Alright, I figured out a fix/workaround so in lieu of getting an answer as to how to print messages to the python error popup window...

Typically my errors/warnings/notifications are coded as the following so wherever I happen to run the code, I'll always see the messages

 def error(msg: str, abort=False):
        arcpy.AddError(msg)  # so it shows up in the GP tool messages
        print(msg)  # so it shows up in console/notebook
        if abort:
            sys.exit()  # immediately terminates if desired

 

Since it appears neither AddMessage nor print() work when checking the syntax, instead you can just raise an Exception with a custom message string instead. This will terminate execution though (which in this specific case you wouldn't mind) but at least you can catch a proper error now.

try:
    do_something()
except:  # shouldn't use a bare except but whatever
    raise Exception('something went horribly wrong')
    # execution terminates and the python error popup window displays the exception message
AlfredBaldenweck
MVP Regular Contributor

Oh, nice. Sorry about the link; I was not reading carefully. Glad you figured it out.

0 Kudos