Skip to content

Commit 98ca00d

Browse files
authored
Merge pull request pallets#4720 from pallets/deprecate-env
deprecate `FLASK_ENV` and `app.env`
2 parents 3e5ca29 + 30427a2 commit 98ca00d

14 files changed

+201
-323
lines changed

CHANGES.rst

+7-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ Unreleased
2323
``g`` instead using a unique prefix, like
2424
``g._extension_name_attr``.
2525

26+
- The ``FLASK_ENV`` environment variable and ``app.env`` attribute are
27+
deprecated, removing the distinction between development and debug
28+
mode. Debug mode should be controlled directly using the ``--debug``
29+
option or ``app.run(debug=True)``. :issue:`4714`
2630
- Add new customization points to the ``Flask`` app object for many
2731
previously global behaviors.
2832

@@ -60,9 +64,9 @@ Unreleased
6064
instance on every request. :issue:`2520`.
6165
- A ``flask.cli.FlaskGroup`` Click group can be nested as a
6266
sub-command in a custom CLI. :issue:`3263`
63-
- Add ``--app``, ``--env``, and ``--debug`` options to the ``flask``
64-
CLI, instead of requiring that they are set through environment
65-
variables. :issue:`2836`
67+
- Add ``--app`` and ``--debug`` options to the ``flask`` CLI, instead
68+
of requiring that they are set through environment variables.
69+
:issue:`2836`
6670
- Add ``--env-file`` option to the ``flask`` CLI. This allows
6771
specifying a dotenv file to load in addition to ``.env`` and
6872
``.flaskenv``. :issue:`3108`

docs/_static/pycharm-run-config.png

75.6 KB
Loading

docs/_static/pycharm-runconfig.png

-16.8 KB
Binary file not shown.

docs/cli.rst

+49-90
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Run the Development Server
7171
The :func:`run <cli.run_command>` command will start the development server. It
7272
replaces the :meth:`Flask.run` method in most cases. ::
7373

74-
$ flask run
74+
$ flask --app hello run
7575
* Serving Flask app "hello"
7676
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
7777

@@ -86,80 +86,56 @@ server tries to start. See :ref:`address-already-in-use` for how to
8686
handle that.
8787

8888

89-
Open a Shell
90-
------------
91-
92-
To explore the data in your application, you can start an interactive Python
93-
shell with the :func:`shell <cli.shell_command>` command. An application
94-
context will be active, and the app instance will be imported. ::
95-
96-
$ flask shell
97-
Python 3.10.0 (default, Oct 27 2021, 06:59:51) [GCC 11.1.0] on linux
98-
App: example [production]
99-
Instance: /home/david/Projects/pallets/flask/instance
100-
>>>
101-
102-
Use :meth:`~Flask.shell_context_processor` to add other automatic imports.
103-
104-
105-
Environments
106-
------------
107-
108-
.. versionadded:: 1.0
89+
Debug Mode
90+
~~~~~~~~~~
10991

110-
The environment in which the Flask app executes is set by the
111-
``FLASK_ENV`` environment variable. When using the ``flask`` command, it
112-
can also be set with the ``--env`` option. If not set it defaults to
113-
``production``. The other recognized environment is ``development``.
114-
Flask and extensions may choose to enable behaviors based on the
115-
environment.
92+
In debug mode, the ``flask run`` command will enable the interactive debugger and the
93+
reloader by default, and make errors easier to see and debug. To enable debug mode, use
94+
the ``--debug`` option.
11695

117-
If the env is set to ``development``, the ``flask`` command will enable
118-
debug mode and ``flask run`` will enable the interactive debugger and
119-
reloader.
96+
.. code-block:: console
12097
121-
.. code-block:: text
122-
123-
$ flask --app hello --env development run
98+
$ flask --app hello --debug run
12499
* Serving Flask app "hello"
125-
* Environment: development
126100
* Debug mode: on
127101
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
128102
* Restarting with inotify reloader
129103
* Debugger is active!
130104
* Debugger PIN: 223-456-919
131105
132106
133-
Watch Extra Files with the Reloader
134-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107+
Watch and Ignore Files with the Reloader
108+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
135109

136-
When using development mode, the reloader will trigger whenever your
137-
Python code or imported modules change. The reloader can watch
138-
additional files with the ``--extra-files`` option. Multiple paths are
139-
separated with ``:``, or ``;`` on Windows.
110+
When using debug mode, the reloader will trigger whenever your Python code or imported
111+
modules change. The reloader can watch additional files with the ``--extra-files``
112+
option. Multiple paths are separated with ``:``, or ``;`` on Windows.
140113

141114
.. code-block:: text
142115
143116
$ flask run --extra-files file1:dirA/file2:dirB/
144117
* Running on http://127.0.0.1:8000/
145118
* Detected change in '/path/to/file1', reloading
146119
120+
The reloader can also ignore files using :mod:`fnmatch` patterns with the
121+
``--exclude-patterns`` option. Multiple patterns are separated with ``:``, or ``;`` on
122+
Windows.
147123

148-
Ignore files with the Reloader
149-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150124

151-
The reloader can also ignore files using :mod:`fnmatch` patterns with
152-
the ``--exclude-patterns`` option. Multiple patterns are separated with
153-
``:``, or ``;`` on Windows.
125+
Open a Shell
126+
------------
154127

128+
To explore the data in your application, you can start an interactive Python
129+
shell with the :func:`shell <cli.shell_command>` command. An application
130+
context will be active, and the app instance will be imported. ::
155131

156-
Debug Mode
157-
----------
132+
$ flask shell
133+
Python 3.10.0 (default, Oct 27 2021, 06:59:51) [GCC 11.1.0] on linux
134+
App: example [production]
135+
Instance: /home/david/Projects/pallets/flask/instance
136+
>>>
158137

159-
Debug mode will be enabled when the execution environment is
160-
``development``, as described above. If you want to control debug mode
161-
separately, use the ``--debug/--no-debug`` option or the ``FLASK_DEBUG``
162-
environment variable.
138+
Use :meth:`~Flask.shell_context_processor` to add other automatic imports.
163139

164140

165141
.. _dotenv:
@@ -552,53 +528,36 @@ script is available. Note that you don't need to set ``--app``. ::
552528
PyCharm Integration
553529
-------------------
554530

555-
PyCharm Professional provides a special Flask run configuration. For
556-
the Community Edition, we need to configure it to call the ``flask run``
557-
CLI command with the correct environment variables. These instructions
558-
should be similar for any other IDE you might want to use.
531+
PyCharm Professional provides a special Flask run configuration to run the development
532+
server. For the Community Edition, and for other commands besides ``run``, you need to
533+
create a custom run configuration. These instructions should be similar for any other
534+
IDE you use.
559535

560-
In PyCharm, with your project open, click on *Run* from the menu bar and
561-
go to *Edit Configurations*. You'll be greeted by a screen similar to
562-
this:
536+
In PyCharm, with your project open, click on *Run* from the menu bar and go to *Edit
537+
Configurations*. You'll see a screen similar to this:
563538

564-
.. image:: _static/pycharm-runconfig.png
539+
.. image:: _static/pycharm-run-config.png
565540
:align: center
566541
:class: screenshot
567-
:alt: Screenshot of PyCharms's run configuration settings.
568-
569-
There's quite a few options to change, but once we've done it for one
570-
command, we can easily copy the entire configuration and make a single
571-
tweak to give us access to other commands, including any custom ones you
572-
may implement yourself.
573-
574-
Click the + (*Add New Configuration*) button and select *Python*. Give
575-
the configuration a name such as "flask run". For the ``flask run``
576-
command, check "Single instance only" since you can't run the server
577-
more than once at the same time.
542+
:alt: Screenshot of PyCharm run configuration.
578543

579-
Select *Module name* from the dropdown (**A**) then input ``flask``.
544+
Once you create a configuration for the ``flask run``, you can copy and change it to
545+
call any other command.
580546

581-
The *Parameters* field (**B**) is set to the CLI command to execute
582-
(with any arguments). In this example we use ``run``, which will run
583-
the development server.
547+
Click the *+ (Add New Configuration)* button and select *Python*. Give the configuration
548+
a name such as "flask run".
584549

585-
You can skip this next step if you're using :ref:`dotenv`. We need to
586-
add an environment variable (**C**) to identify our application. Click
587-
on the browse button and add an entry with ``FLASK_APP`` on the left and
588-
the Python import or file on the right (``hello`` for example). Add an
589-
entry with ``FLASK_ENV`` and set it to ``development``.
550+
Click the *Script path* dropdown and change it to *Module name*, then input ``flask``.
590551

591-
Next we need to set the working directory (**D**) to be the folder where
592-
our application resides.
552+
The *Parameters* field is set to the CLI command to execute along with any arguments.
553+
This example uses ``--app hello --debug run``, which will run the development server in
554+
debug mode. ``--app hello`` should be the import or file with your Flask app.
593555

594-
If you have installed your project as a package in your virtualenv, you
595-
may untick the *PYTHONPATH* options (**E**). This will more accurately
596-
match how you deploy the app later.
556+
If you installed your project as a package in your virtualenv, you may uncheck the
557+
*PYTHONPATH* options. This will more accurately match how you deploy later.
597558

598-
Click *Apply* to save the configuration, or *OK* to save and close the
599-
window. Select the configuration in the main PyCharm window and click
600-
the play button next to it to run the server.
559+
Click *OK* to save and close the configuration. Select the configuration in the main
560+
PyCharm window and click the play button next to it to run the server.
601561

602-
Now that we have a configuration which runs ``flask run`` from within
603-
PyCharm, we can copy that configuration and alter the *Script* argument
604-
to run a different CLI command, e.g. ``flask shell``.
562+
Now that you have a configuration for ``flask run``, you can copy that configuration and
563+
change the *Parameters* argument to run a different CLI command.

docs/config.rst

+25-43
Original file line numberDiff line numberDiff line change
@@ -42,38 +42,22 @@ method::
4242
)
4343

4444

45-
Environment and Debug Features
46-
------------------------------
45+
Debug Mode
46+
----------
4747

48-
The :data:`ENV` and :data:`DEBUG` config values are special because they
49-
may behave inconsistently if changed after the app has begun setting up.
50-
In order to set the environment and debug mode reliably, pass options to
51-
the ``flask`` command or use environment variables.
52-
53-
The execution environment is used to indicate to Flask, extensions, and
54-
other programs, like Sentry, what context Flask is running in. It is
55-
controlled with the ``FLASK_ENV`` environment variable, or the
56-
``--env`` option when using the ``flask`` command, and defaults to
57-
``production``.
58-
59-
Setting ``--env development`` will enable debug mode. ``flask run`` will
60-
use the interactive debugger and reloader by default in debug mode. To
61-
control this separately from the environment, use the
62-
``--debug/--no-debug`` option or the ``FLASK_DEBUG`` environment
63-
variable.
64-
65-
To switch Flask to the development environment and enable debug mode,
66-
set ``--env``:
48+
The :data:`DEBUG` config value is special because it may behave inconsistently if
49+
changed after the app has begun setting up. In order to set debug mode reliably, use the
50+
``--debug`` option on the ``flask`` command.``flask run`` will use the interactive
51+
debugger and reloader by default in debug mode.
6752

6853
.. code-block:: text
6954
70-
$ flask --app hello --env development run
55+
$ flask --app hello --debug run
7156
72-
Using the options or environment variables as described above is
73-
recommended. While it is possible to set :data:`ENV` and :data:`DEBUG`
74-
in your config or code, this is strongly discouraged. They can't be read
75-
early by the ``flask`` command, and some systems or extensions may have
76-
already configured themselves based on a previous value.
57+
Using the option is recommended. While it is possible to set :data:`DEBUG` in your
58+
config or code, this is strongly discouraged. It can't be read early by the ``flask``
59+
command, and some systems or extensions may have already configured themselves based on
60+
a previous value.
7761

7862

7963
Builtin Configuration Values
@@ -83,32 +67,27 @@ The following configuration values are used internally by Flask:
8367

8468
.. py:data:: ENV
8569
86-
What environment the app is running in. Flask and extensions may
87-
enable behaviors based on the environment, such as enabling debug
88-
mode. The :attr:`~flask.Flask.env` attribute maps to this config
89-
key. This is set by the :envvar:`FLASK_ENV` environment variable and
90-
may not behave as expected if set in code.
91-
92-
**Do not enable development when deploying in production.**
70+
What environment the app is running in. The :attr:`~flask.Flask.env` attribute maps
71+
to this config key.
9372

9473
Default: ``'production'``
9574

75+
.. deprecated:: 2.2
76+
Will be removed in Flask 2.3. Use ``--debug`` instead.
77+
9678
.. versionadded:: 1.0
9779

9880
.. py:data:: DEBUG
9981
100-
Whether debug mode is enabled. When using ``flask run`` to start the
101-
development server, an interactive debugger will be shown for
102-
unhandled exceptions, and the server will be reloaded when code
103-
changes. The :attr:`~flask.Flask.debug` attribute maps to this
104-
config key. This is enabled when :data:`ENV` is ``'development'``
105-
and is overridden by the ``FLASK_DEBUG`` environment variable. It
106-
may not behave as expected if set in code.
82+
Whether debug mode is enabled. When using ``flask run`` to start the development
83+
server, an interactive debugger will be shown for unhandled exceptions, and the
84+
server will be reloaded when code changes. The :attr:`~flask.Flask.debug` attribute
85+
maps to this config key. This is set with the ``FLASK_DEBUG`` environment variable.
86+
It may not behave as expected if set in code.
10787

10888
**Do not enable debug mode when deploying in production.**
10989

110-
Default: ``True`` if :data:`ENV` is ``'development'``, or ``False``
111-
otherwise.
90+
Default: ``False``
11291

11392
.. py:data:: TESTING
11493
@@ -408,6 +387,9 @@ The following configuration values are used internally by Flask:
408387
removed in Flask 2.3. The default ``app.json`` provider has
409388
equivalent attributes instead.
410389

390+
.. versionchanged:: 2.2
391+
``ENV`` will be removed in Flask 2.3. Use ``--debug`` instead.
392+
411393

412394
Configuring from Python Files
413395
-----------------------------

docs/debugging.rst

+8-14
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,22 @@ during a request. This debugger should only be used during development.
3939
security risk. Do not run the development server or debugger in a
4040
production environment.
4141

42-
To enable the debugger, run the development server with the environment
43-
set to ``development``. This puts Flask in debug mode, which changes how
44-
it handles some errors, and enables the debugger and reloader.
42+
The debugger is enabled by default when the development server is run in debug mode.
4543

4644
.. code-block:: text
4745
48-
$ flask --app hello --env development run
46+
$ flask --app hello --debug run
4947
50-
``FLASK_ENV`` can also be set as an environment variable. When running
51-
from Python code, passing ``debug=True`` enables debug mode, which is
52-
mostly equivalent. Debug mode can be controlled separately from the
53-
environment with the ``--debug/--no-debug`` option or the
54-
``FLASK_DEBUG`` environment variable.
48+
When running from Python code, passing ``debug=True`` enables debug mode, which is
49+
mostly equivalent.
5550

5651
.. code-block:: python
5752
5853
app.run(debug=True)
5954
60-
:doc:`/server` and :doc:`/cli` have more information about running the
61-
debugger, debug mode, and development mode. More information about the
62-
debugger can be found in the `Werkzeug documentation
63-
<https://werkzeug.palletsprojects.com/debug/>`__.
55+
:doc:`/server` and :doc:`/cli` have more information about running the debugger and
56+
debug mode. More information about the debugger can be found in the `Werkzeug
57+
documentation <https://werkzeug.palletsprojects.com/debug/>`__.
6458

6559

6660
External Debuggers
@@ -78,7 +72,7 @@ which can interfere.
7872

7973
.. code-block:: text
8074
81-
$ flask --app hello --env development run --no-debugger --no-reload
75+
$ flask --app hello --debug run --no-debugger --no-reload
8276
8377
When running from Python:
8478

docs/quickstart.rst

+3-6
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,12 @@ error occurs during a request.
104104
security risk. Do not run the development server or debugger in a
105105
production environment.
106106

107-
To enable all development features, set the ``--env`` option to
108-
``development``.
107+
To enable debug mode, use the ``--debug`` option.
109108

110109
.. code-block:: text
111110
112-
$ flask --app hello --env development run
111+
$ flask --app hello --debug run
113112
* Serving Flask app 'hello'
114-
* Environment: development
115113
* Debug mode: on
116114
* Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
117115
* Restarting with stat
@@ -120,8 +118,7 @@ To enable all development features, set the ``--env`` option to
120118
121119
See also:
122120

123-
- :doc:`/server` and :doc:`/cli` for information about running in
124-
development mode.
121+
- :doc:`/server` and :doc:`/cli` for information about running in debug mode.
125122
- :doc:`/debugging` for information about using the built-in debugger
126123
and other debuggers.
127124
- :doc:`/logging` and :doc:`/errorhandling` to log errors and display

0 commit comments

Comments
 (0)