Merge pull request #2593 from pallets/env-docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
clean up FLASK_ENV docs
authored by David Lord · committed by GitHub · parents 2949306, 87c2e12 · Browse files · 7 files·+129 added−83 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+129 added−83 removed
- other:+129 added−83 removed
Provenance
- Commit
c3a9978- Tree
dab20a9- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
2949306- Parent 2
87c2e12
7 files changed+129 added−83 removed
modified:docs/tutorial/packaging.rst+2 added-2 removed
80 unmodified lines81 export FLASK_ENV=development82 flask run83 84 (In case you are on Windows you need to use ``set`` instead of ``export``).85-The :envvar:`FLASK_ENV` flag if set to ``development`` turns on all86-development features such as enabling the interactive debugger.85+Exporting ``FLASK_ENV=development`` turns on all development features86+such as enabling the interactive debugger.87 88 *Never leave debug mode activated in a production system*, because it will89 allow users to execute code on the server!90 18 unmodified linesmodified:docs/cli.rst+20 added-15 removed
114 unmodified lines115 >>>116 117 Use :meth:`~Flask.shell_context_processor` to add other automatic imports.118 119+120 Environments121 ------------122 123 .. versionadded:: 1.0124 124-The environment in which the Flask app should run is set by the125-:envvar:`FLASK_ENV` environment variable. If not set it defaults to126-``production``. The other default environment which is known is127-``development``. If the env is set to ``development`` the debug mode is128-for instance automatically enabled.125+The environment in which the Flask app runs is set by the126+:envvar:`FLASK_ENV` environment variable. If not set it defaults to127+``production``. The other recognized environment is ``development``.128+Flask and extensions may choose to enable behaviors based on the129+environment.130 130-Debug Mode131-----------131+If the env is set to ``development``, the ``flask`` command will enable132+debug mode and ``flask run`` will enable the interactive debugger and133+reloader.134 133-Set the :envvar:`FLASK_DEBUG` environment variable to override the134-application's :attr:`~Flask.debug` flag. The value ``1`` enables it, ``0``135-disables it. Forcing the debug flag on also enables the debugger and reloader136-when running the development server.137-135 ::136 140- $ FLASK_DEBUG=1 flask run137+ $ FLASK_ENV=development flask run138 * Serving Flask app "hello"142- * Forcing debug mode on143- * Env production139+ * Environment: development140+ * Debug mode: on141 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)142 * Restarting with inotify reloader143 * Debugger is active!144 * Debugger PIN: 223-456-919145 146 147+Debug Mode148+----------149+150+Debug mode will be enabled when :envvar:`FLASK_ENV` is ``development``,151+as described above. If you want to control debug mode separately, use152+:envvar:`FLASK_DEBUG`. The value ``1`` enables it, ``0`` disables it.153+154+155 .. _dotenv:156 157 Environment Variables From dotenv158 ---------------------------------253 unmodified linesmodified:docs/config.rst+57 added-27 removed
18 unmodified lines19 object. This is the place where Flask itself puts certain configuration20 values and also where extensions can put their configuration values. But21 this is also where you can have your own configuration.22 23+24 Configuration Basics25 --------------------26 27 The :attr:`~flask.Flask.config` is actually a subclass of a dictionary and14 unmodified lines42 TESTING=True,43 SECRET_KEY=b'_5#y2L"F4Q8z\n\xec]/'44 )45 46+47 Environment and Debug Features48 ------------------------------49 48-Some values are special in that they can show unexpected behavior when49-changed late. In particular that applies to the Flask environment and50-debug mode.50+The :data:`ENV` and :data:`DEBUG` config values are special because they51+may behave inconsistently if changed after the app has begun setting up.52+In order to set the environment and debug mode reliably, Flask uses53+environment variables.54 52-If you use the :command:`flask` script to start a local development server53-for instance you should tell Flask that you want to work in the54-development environment. For safety reasons we default the flask55-environment to production mode instead of development. This is done56-because development mode can turn on potentially unsafe features such as57-the debugger by default.55+The environment is used to indicate to Flask, extensions, and other56+programs, like Sentry, what context Flask is running in. It is57+controlled with the :envvar:`FLASK_ENV` environment variable and58+defaults to ``production``.59 59-To control the environment and such fundamental features Flask provides60-the two environment variables :envvar:`FLASK_ENV` and :envvar:`FLASK_DEBUG`.61-In versions of Flask older than 1.0 the :envvar:`FLASK_ENV` environment62-variable did not exist.60+Setting :envvar:`FLASK_ENV` to ``development`` will enable debug mode.61+``flask run`` will use the interactive debugger and reloader by default62+in debug mode. To control this separately from the environment, use the63+:envvar:`FLASK_DEBUG` flag.64 64-The most common way to switch Flask to development mode is to tell it to65-work on the ``development`` environment::65+.. versionchanged:: 1.066+ Added :envvar:`FLASK_ENV` to control the environment separately67+ from debug mode. The development environment enables debug mode.68 67-$ export FLASK_ENV=development68-$ flask run69+To switch Flask to the development environment and enable debug mode,70+set :envvar:`FLASK_ENV`::71 70-(On Windows you need to use ``set`` instead of ``export``).72+ $ export FLASK_ENV=development73+ $ flask run74 72-While you can attempt to flip the environment and debug flag separately in73-the Flask config from the config file this is strongly discouraged as74-those flags are often loaded early and changing them late might not apply75-to all systems and extensions.75+(On Windows, use ``set`` instead of ``export``.)76 77+Using the environment variables as described above is recommended. While78+it is possible to set :data:`ENV` and :data:`DEBUG` in your config or79+code, this is strongly discouraged. They can't be read early by the80+``flask`` command, and some systems or extensions may have already81+configured themselves based on a previous value.82+83+84 Builtin Configuration Values85 ----------------------------86 87 The following configuration values are used internally by Flask:88 89+.. py:data:: ENV90+91+ What environment the app is running in. Flask and extensions may92+ enable behaviors based on the environment, such as enabling debug93+ mode. The :attr:`~flask.Flask.env` attribute maps to this config94+ key. This is set by the :envvar:`FLASK_ENV` environment variable and95+ may not behave as expected if set in code.96+97+ **Do not enable development when deploying in production.**98+99+ Default: ``'production'``100+101+ .. versionadded:: 1.0102+103 .. py:data:: DEBUG104 84- Enable debug mode. When using the development server with ``flask run`` or85- ``app.run``, an interactive debugger will be shown for unhanlded86- exceptions, and the server will be reloaded when code changes.105+ Whether debug mode is enabled. When using ``flask run`` to start the106+ development server, an interactive debugger will be shown for107+ unhandled exceptions, and the server will be reloaded when code108+ changes. The :attr:`~flask.Flask.debug` attribute maps to this109+ config key. This is enabled when :data:`ENV` is ``'development'``110+ and is overridden by the ``FLASK_DEBUG`` environment variable. It111+ may not behave as expected if set in code.112 88- **Do not enable debug mode in production.**113+ **Do not enable debug mode when deploying in production.**114 90- Default: ``False``115+ Default: ``True`` if :data:`ENV` is ``'production'``, or ``False``116+ otherwise.117 118 .. py:data:: TESTING119 120 Enable testing mode. Exceptions are propagated rather than handled by the243 unmodified lines364 365 ``LOGGER_NAME`` and ``LOGGER_HANDLER_POLICY`` were removed. See366 :ref:`logging` for information about configuration.367 368+ Added :data:`ENV` to reflect the :envvar:`FLASK_ENV` environment369+ variable.370+371+372 Configuring from Files373 ----------------------374 375 Configuration becomes more useful if you can store it in a separate file,263 unmodified linesmodified:docs/quickstart.rst+6 added-5 removed
128 unmodified lines129 That is not very nice and Flask can do better. If you enable debug130 support the server will reload itself on code changes, and it will also131 provide you with a helpful debugger if things go wrong.132 133-To enable all development features (and to disable the debug mode) you can134-export the ``FLASK_ENV`` environment variable and set it to135-``development``133+To enable all development features (including debug mode) you can export134+the ``FLASK_ENV`` environment variable and set it to ``development``135 before running the server::136 137 $ export FLASK_ENV=development138 $ flask run139 141-(On Windows you need to use ``set`` instead of ``export`` and on Flask142-versions older than 1.0 you need to export ``FLASK_DEBUG=1`` instead).140+(On Windows you need to use ``set`` instead of ``export``.)141 142 This does the following things:143 144 1. it activates the debugger145 2. it activates the automatic reloader146 3. it enables the debug mode on the Flask application.147 148+You can also control debug mode separately from the environment by149+exporting ``FLASK_DEBUG=1``.150+151 There are more parameters that are explained in the :ref:`server` docs.152 153 .. admonition:: Attention154 696 unmodified linesmodified:docs/server.rst+8 added-6 removed
19 unmodified lines20 $ export FLASK_APP=my_application21 $ export FLASK_ENV=development22 $ flask run23 24-This will enable the debugger, the reloader and then start the server on24+This enables the development environment, including the interactive25+debugger and reloader, and then starts the server on26 *http://localhost:5000/*.27 28 The individual features of the server can be controlled by passing more28-arguments to the ``run`` option. For instance the reloader can be29+arguments to the ``run`` option. For instance the reloader can be30 disabled::31 32 $ flask run --no-reload33 34 .. note::35 35- On older Flask version (before 1.0) the :envvar:`FLASK_ENV`36- environment variable is not supported and you need to enable the37- debug mode separately by setting the :envvar:`FLASK_DEBUG` environment38- variable to ``1``.36+ Prior to Flask 1.0 the :envvar:`FLASK_ENV` environment variable was37+ not supported and you needed to enable debug mode by exporting38+ ``FLASK_DEBUG=1``. This can still be used to control debug mode, but39+ you should prefer setting the development environment as shown40+ above.41 42 In Code43 -------44 18 unmodified linesmodified:flask/app.py+23 added-20 removed
783 unmodified lines784 for processor in self.shell_context_processors:785 rv.update(processor())786 return rv787 788- #: The environment value. This is typically set from outside the789- #: process by setting the `FLASK_ENV` environment variable and can be790- #: used to quickly switch between different environments like791- #: `production` and `development`. If not set this defaults to792- #: `production`.788+ #: What environment the app is running in. Flask and extensions may789+ #: enable behaviors based on the environment, such as enabling debug790+ #: mode. This maps to the :data:`ENV` config key. This is set by the791+ #: :envvar:`FLASK_ENV` environment variable and may not behave as792+ #: expected if set in code.793+ #:794+ #: **Do not enable development when deploying in production.**795+ #:796+ #: Default: ``'production'``797 env = ConfigAttribute('ENV')798 799 def _get_debug(self):800 return self.config['DEBUG']801+802 def _set_debug(self, value):798- self._set_debug_value(value)803+ self.config['DEBUG'] = value804+ self.jinja_env.auto_reload = self.templates_auto_reload805 800- #: The debug flag. If this is ``True`` it enables debugging of the801- #: application. In debug mode the debugger will kick in when an802- #: unhandled exception occurs and the integrated server will803- #: automatically reload the application if changes in the code are804- #: detected.806+ #: Whether debug mode is enabled. When using ``flask run`` to start807+ #: the development server, an interactive debugger will be shown for808+ #: unhandled exceptions, and the server will be reloaded when code809+ #: changes. This maps to the :data:`DEBUG` config key. This is810+ #: enabled when :attr:`env` is ``'development'`` and is overridden811+ #: by the ``FLASK_DEBUG`` environment variable. It may not behave as812+ #: expected if set in code.813 #:806- #: This value should only be configured by the :envvar:`FLASK_DEBUG`807- #: environment variable. Changing it by other means will not yield808- #: consistent results. The default value depends on the Flask809- #: environment and will be true for the development environment and false810- #: otherwise.814+ #: **Do not enable debug mode when deploying in production.**815+ #:816+ #: Default: ``True`` if :attr:`env` is ``'development'``, or817+ #: ``False`` otherwise.818 debug = property(_get_debug, _set_debug)819 del _get_debug, _set_debug820 814- def _set_debug_value(self, value):815- self.config['DEBUG'] = value816- self.jinja_env.auto_reload = self.templates_auto_reload817-821 def run(self, host=None, port=None, debug=None,822 load_dotenv=True, **options):823 """Runs the application on a local development server.824 1388 unmodified linesmodified:flask/helpers.py+13 added-8 removed
45 unmodified lines46 if sep not in (None, '/'))47 48 49 def get_env():50- val = os.environ.get('FLASK_ENV')51- if not val:52- val = 'production'53- return val50+ """Get the environment the app is running in, indicated by the51+ :envvar:`FLASK_ENV` environment variable. The default is52+ ``'production'``.53+ """54+ return os.environ.get('FLASK_ENV') or 'production'55 56 57 def get_debug_flag():58+ """Get whether debug mode should be enabled for the app, indicated59+ by the :envvar:`FLASK_DEBUG` environment variable. The default is60+ ``True`` if :func:`.get_env` returns ``'development'``, or ``False``61+ otherwise.62+ """63 val = os.environ.get('FLASK_DEBUG')64+65 if not val:59- env = get_env()60- if env == 'development':61- return True62- return False66+ return get_env() == 'development'67+68 return val.lower() not in ('0', 'false', 'no')69 70 71 def _endpoint_from_view_func(view_func):963 unmodified lines