debugging.rst
This file
Path
docs/debugging.rst
147 lines · 4.1 KiB
Last changed by
Merge branch '2.0.x'David Lordbefore adoptione37e871
History of this fileTake it
The walk stopped after 200 commits: lines older than 1035efc point at it.
Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 1–55why, one step back1Debugging 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 run55Merge branch '2.0.x'David Lordbefore adoptione37e871
lines 56–62why, one step back56 .. group-tab:: Fish5758 .. code-block:: text5960 $ set -x FLASK_ENV development61 $ flask run62Add command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 63–78why, one step back63 .. group-tab:: CMD6465 .. code-block:: text6667 > set FLASK_ENV=development68 > flask run6970 .. group-tab:: Powershell7172 .. code-block:: text7374 > $env:FLASK_ENV = "development"75 > flask run7677``FLASK_ENV`` can only be set as an environment variable. When running78from Python code, passing ``debug=True`` enables debug mode, which isMerge branch '2.0.x'David Lordbefore adoptionf4a2c35
line 79why, one step back79mostly equivalent. Debug mode can be controlled separately fromAdd command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 80–113why, one step back80``FLASK_ENV`` with the ``FLASK_DEBUG`` environment variable as well.8182.. code-block:: python8384 app.run(debug=True)8586:doc:`/server` and :doc:`/cli` have more information about running the87debugger, debug mode, and development mode. More information about the88debugger can be found in the `Werkzeug documentation89<https://werkzeug.palletsprojects.com/debug/>`__.909192External Debuggers93------------------9495External debuggers, such as those provided by IDEs, can offer a more96powerful debugging experience than the built-in debugger. They can also97be used to step through code during a request before an error is raised,98or if no error is raised. Some even have a remote mode so you can debug99code running on another machine.100101When using an external debugger, the app should still be in debug mode,102but it can be useful to disable the built-in debugger and reloader,103which can interfere.104105When running from the command line:106107.. tabs::108109 .. group-tab:: Bash110111 .. code-block:: text112113 $ export FLASK_ENV=developmentMerge branch '2.0.x'David Lordbefore adoptione37e871
lines 114–120why, one step back114 $ flask run --no-debugger --no-reload115116 .. group-tab:: Fish117118 .. code-block:: text119120 $ set -x FLASK_ENV developmentAdd command switch tabs for Bash, CMD and Powershell with sphinx-tabs (#3714)Grey Libefore adoption1035efc
lines 121–147why, one step back121 $ flask run --no-debugger --no-reload122123 .. group-tab:: CMD124125 .. code-block:: text126127 > set FLASK_ENV=development128 > flask run --no-debugger --no-reload129130 .. group-tab:: Powershell131132 .. code-block:: text133134 > $env:FLASK_ENV = "development"135 > flask run --no-debugger --no-reload136137When running from Python:138139.. code-block:: python140141 app.run(debug=True, use_debugger=False, use_reloader=False)142143Disabling these isn't required, an external debugger will continue to144work with the following caveats. If the built-in debugger is not145disabled, it will catch unhandled exceptions before the external146debugger can. If the reloader is not disabled, it could cause an147unexpected reload if code changes during debugging.