debugging.rst
This file
Path
docs/debugging.rst
97 lines · 3.3 KiB
Last changed by
Merge pull request #3669 from ebonnecab/error-docsDavid Lordbefore adoption36e6fc8
History of this fileTake 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.. code-block:: text4849 $ export FLASK_ENV=development50 $ flask run5152``FLASK_ENV`` can only be set as an environment variable. When running53from Python code, passing ``debug=True`` enables debug mode, which is54mostly equivalent. Debug mode can be controled separately from55``FLASK_ENV`` with the ``FLASK_DEBUG`` environment variable as well.5657.. code-block:: python5859 app.run(debug=True)6061:doc:`/server` and :doc:`/cli` have more information about running the62debugger, debug mode, and development mode. More information about the63debugger can be found in the `Werkzeug documentation64<https://werkzeug.palletsprojects.com/debug/>`__.656667External Debuggers68------------------6970External debuggers, such as those provided by IDEs, can offer a more71powerful debugging experience than the built-in debugger. They can also72be used to step through code during a request before an error is raised,73or if no error is raised. Some even have a remote mode so you can debug74code running on another machine.7576When using an external debugger, the app should still be in debug mode,77but it can be useful to disable the built-in debugger and reloader,78which can interfere.7980When running from the command line:8182.. code-block:: text8384 $ export FLASK_ENV=development85 $ flask run --no-debugger --no-reload8687When running from Python:8889.. code-block:: python9091 app.run(debug=True, use_debugger=False, use_reloader=False)9293Disabling these isn't required, an external debugger will continue to94work with the following caveats. If the built-in debugger is not95disabled, it will catch unhandled exceptions before the external96debugger can. If the reloader is not disabled, it could cause an97unexpected reload if code changes during debugging.