debugging.rst
This file
Path
docs/debugging.rst
93 lines · 3.2 KiB
Last changed by
Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
History of this fileTake it
The walk stopped after 200 commits: lines older than e783424 point at it.
Merge pull request #3888 from pallets/dependabot/pip/sphinx-3.4.3dependabot-preview[bot]before adoptione783424
lines 1–41why, 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.41Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
lines 42–44why, one step back42To enable the debugger, run the development server with the environment43set to ``development``. This puts Flask in debug mode, which changes how44it handles some errors, and enables the debugger and reloader.Merge pull request #3888 from pallets/dependabot/pip/sphinx-3.4.3dependabot-preview[bot]before adoptione783424
line 45why, one step back45Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
line 46why, one step back46.. code-block:: textMerge pull request #3888 from pallets/dependabot/pip/sphinx-3.4.3dependabot-preview[bot]before adoptione783424
line 47why, one step back47Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
line 48why, one step back48 $ flask --app hello --env development runMerge pull request #3888 from pallets/dependabot/pip/sphinx-3.4.3dependabot-preview[bot]before adoptione783424
line 49why, one step back49Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
line 50why, one step back50``FLASK_ENV`` can also be set as an environment variable. When runningMerge pull request #3888 from pallets/dependabot/pip/sphinx-3.4.3dependabot-preview[bot]before adoptione783424
line 51why, one step back51from Python code, passing ``debug=True`` enables debug mode, which isMerge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
lines 52–54why, one step back52mostly equivalent. Debug mode can be controlled separately from the53environment with the ``--debug/--no-debug`` option or the54``FLASK_DEBUG`` environment variable.Merge pull request #3888 from pallets/dependabot/pip/sphinx-3.4.3dependabot-preview[bot]before adoptione783424
lines 55–78why, one step back5556.. code-block:: python5758 app.run(debug=True)5960:doc:`/server` and :doc:`/cli` have more information about running the61debugger, debug mode, and development mode. More information about the62debugger can be found in the `Werkzeug documentation63<https://werkzeug.palletsprojects.com/debug/>`__.646566External Debuggers67------------------6869External debuggers, such as those provided by IDEs, can offer a more70powerful debugging experience than the built-in debugger. They can also71be used to step through code during a request before an error is raised,72or if no error is raised. Some even have a remote mode so you can debug73code running on another machine.7475When using an external debugger, the app should still be in debug mode,76but it can be useful to disable the built-in debugger and reloader,77which can interfere.78Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
line 79why, one step back79.. code-block:: textMerge pull request #3888 from pallets/dependabot/pip/sphinx-3.4.3dependabot-preview[bot]before adoptione783424
line 80why, one step back80Merge pull request #4646 from pallets/cli-app-envDavid Lordbefore adoptionae54727
line 81why, one step back81 $ flask --app hello --env development run --no-debugger --no-reloadMerge pull request #3888 from pallets/dependabot/pip/sphinx-3.4.3dependabot-preview[bot]before adoptione783424
lines 82–93why, one step back8283When running from Python:8485.. code-block:: python8687 app.run(debug=True, use_debugger=False, use_reloader=False)8889Disabling these isn't required, an external debugger will continue to90work with the following caveats. If the built-in debugger is not91disabled, it will catch unhandled exceptions before the external92debugger can. If the reloader is not disabled, it could cause an93unexpected reload if code changes during debugging.