Debugging Applications

Depending on the WSGI gateway/server, exceptions are handled differently.But most of the time, exceptions go to stderr or the error log.

Since this is not the best debugging environment, Werkzeug provides aWSGI middleware that renders nice debugging tracebacks, optionally with anAJAX based debugger (which allows to execute code in the context of thetraceback’s frames).

The interactive debugger however does not work in forking environmentswhich makes it nearly impossible to use on production servers. Also thedebugger allows the execution of arbitrary code which makes it a majorsecurity risk and must never be used on production machines because ofthat.

Enabling the Debugger

You can enable the debugger by wrapping the application in aDebuggedApplication middleware. Additionally there areparameters to the run_simple() function to enable it because thisis a common task during development.

  • class _werkzeug.debug.DebuggedApplication(_app, evalex=False, request_key='werkzeug.request', console_path='/console', console_init_func=None, show_hidden_frames=False, lodgeit_url=None)
  • Enables debugging support for a given application:
  1. from werkzeug.debug import DebuggedApplication
  2. from myapp import app
  3. app = DebuggedApplication(app, evalex=True)

The evalex keyword argument allows evaluating expressions in atraceback’s frame context.

0.9 新版功能: The lodgeit_url parameter was deprecated.

参数:

  • app – the WSGI application to run debugged.
  • evalex – enable exception evaluation feature (interactivedebugging). This requires a non-forking server.
  • request_key – The key that points to the request object in thsenvironment. This parameter is ignored in currentversions.
  • console_path – the URL for a general purpose console.
  • console_init_func – the function that is executed before startingthe general purpose console. The return valueis used as initial namespace.
  • show_hidden_frames – by default hidden traceback frames are skipped.You can show them by setting this parameterto True.

Using the Debugger

Once enabled and an error happens during a request you will see a detailedtraceback instead of a general “internal server error”. If you have theevalex feature enabled you can also get a traceback for every frame inthe traceback by clicking on the console icon.

Once clicked a console opens where you can execute Python code in:a screenshot of the interactive debuggerInside the interactive consoles you can execute any kind of Python code.Unlike regular Python consoles the output of the object reprs is coloredand stripped to a reasonable size by default. If the output is longerthan what the console decides to display a small plus sign is added tothe repr and a click will expand the repr.

To display all variables that are defined in the current frame you canuse the dump() function. You can call it without arguments to get adetailed list of all variables and their values, or with an object asargument to get a detailed list of all the attributes it has.

Pasting Errors

If you click on the Traceback title the traceback switches over to a textbased one. The text based one can be pasted to paste.pocoo.org with oneclick.