Skip to content
Myr

debugging.rst

This file

Path

docs/debugging.rst

133 lines · 3.9 KiB

Last changed by

Merge branch '2.0.x'David Lordbefore adoptionf4a2c35

History of this file

Take it

1Debugging Application Errors2============================345In Production6-------------78**Do not run the development server, or enable the built-in debugger, in9a production environment.** The debugger allows executing arbitrary10Python code from the browser. It's protected by a pin, but that should11not be relied on for security.1213Use an error logging tool, such as Sentry, as described in14:ref:`error-logging-tools`, or enable logging and notifications as15described in :doc:`/logging`.1617If you have access to the server, you could add some code to start an18external debugger if ``request.remote_addr`` matches your IP. Some IDE19debuggers also have a remote mode so breakpoints on the server can be20interacted with locally. Only enable a debugger temporarily.212223The Built-In Debugger24---------------------2526The built-in Werkzeug development server provides a debugger which shows27an interactive traceback in the browser when an unhandled error occurs28during a request. This debugger should only be used during development.2930.. image:: _static/debugger.png31   :align: center32   :class: screenshot33   :alt: screenshot of debugger in action3435.. warning::3637    The debugger allows executing arbitrary Python code from the38    browser. It is protected by a pin, but still represents a major39    security risk. Do not run the development server or debugger in a40    production environment.4142To enable the debugger, run the development server with the43``FLASK_ENV`` environment variable set to ``development``. This puts44Flask in debug mode, which changes how it handles some errors, and45enables the debugger and reloader.4647.. tabs::4849   .. group-tab:: Bash5051      .. code-block:: text5253         $ export FLASK_ENV=development54         $ flask run5556   .. group-tab:: CMD5758      .. code-block:: text5960         > set FLASK_ENV=development61         > flask run6263   .. group-tab:: Powershell6465      .. code-block:: text6667         > $env:FLASK_ENV = "development"68         > flask run6970``FLASK_ENV`` can only be set as an environment variable. When running71from Python code, passing ``debug=True`` enables debug mode, which is72mostly equivalent. Debug mode can be controlled separately from73``FLASK_ENV`` with the ``FLASK_DEBUG`` environment variable as well.7475.. code-block:: python7677    app.run(debug=True)7879:doc:`/server` and :doc:`/cli` have more information about running the80debugger, debug mode, and development mode. More information about the81debugger can be found in the `Werkzeug documentation82<https://werkzeug.palletsprojects.com/debug/>`__.838485External Debuggers86------------------8788External debuggers, such as those provided by IDEs, can offer a more89powerful debugging experience than the built-in debugger. They can also90be used to step through code during a request before an error is raised,91or if no error is raised. Some even have a remote mode so you can debug92code running on another machine.9394When using an external debugger, the app should still be in debug mode,95but it can be useful to disable the built-in debugger and reloader,96which can interfere.9798When running from the command line:99100.. tabs::101102   .. group-tab:: Bash103104      .. code-block:: text105106         $ export FLASK_ENV=development107         $ flask run --no-debugger --no-reload108109   .. group-tab:: CMD110111      .. code-block:: text112113         > set FLASK_ENV=development114         > flask run --no-debugger --no-reload115116   .. group-tab:: Powershell117118      .. code-block:: text119120         > $env:FLASK_ENV = "development"121         > flask run --no-debugger --no-reload122123When running from Python:124125.. code-block:: python126127    app.run(debug=True, use_debugger=False, use_reloader=False)128129Disabling these isn't required, an external debugger will continue to130work with the following caveats. If the built-in debugger is not131disabled, it will catch unhandled exceptions before the external132debugger can. If the reloader is not disabled, it could cause an133unexpected reload if code changes during debugging.