QA Graphic

View Python Exceptions in the Browser

Quick trick to view errors

To enable debugging your Python CGI, use the following in your Python header:

import cgitb
cgitb.enable()

Now when you run into an error, you will see the error message in your browser. Just be careful not to enable this for production pages. cgitb is part of the standard distribution package.

Official Description

If an uncaught exception occurs, a detailed, formatted report will be displayed. The report includes a traceback showing excerpts of the source code for each level, as well as the values of the arguments and local variables to currently running functions, to help you debug the problem. Optionally, you can save this information to a file instead of sending it to the browser.

Note: This doesn't handle all errors, for example, if you put in an illegal condition statement, you'll see a syntax error in the browser, but will need to run the python script from the command line to see the error.

Turn Off When In Production!

cgitb is a good thing to enable when developing code. Once the code is complete, remove the cgitb reference so that if customers encounter an error they don't see the code.

Python Name Error
Python Error page can show sensitive information!

Change the enable to display the data to a log file. Here's an example entry.

import cgitb
cgitb.enable(logdir=os.path.join(os.path.dirname(__file__), 'Weblog'),
             display=False,
             format='text',
             )

You can always setup a GitHub action to search for the "cgitb.enable()" before posting code into Production.