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.
Solved! Go to Solution.
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
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
and if you click on that warning, it'll tell you exactly the issue(s).
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.
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?
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?
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
Oh, nice. Sorry about the link; I was not reading carefully. Glad you figured it out.