Command Line Interface
Installing Flask installs the flask
script, a Click command line interface, in your virtualenv. Executed from the terminal, this script gives access to built-in, extension, and application-defined commands. The --help
option will give more information about any commands and options.
Application Discovery
The flask
command is installed by Flask, not your application; it must be told where to find your application in order to use it. The FLASK_APP
environment variable is used to specify how to load the application.
Unix Bash (Linux, Mac, etc.):
$ export FLASK_APP=hello
$ flask run
FLASK_APP
has three parts: an optional path that sets the current working directory, a Python file or dotted import path, and an optional variable name of the instance or factory. If the name is a factory, it can optionally be followed by arguments in parentheses. The following values demonstrate these parts:
FLASK_APP=src/hello
- Sets the current working directory to
src
then importshello
. FLASK_APP=hello.web
- Imports the path
hello.web
. FLASK_APP=hello:app2
- Uses the
app2
Flask instance inhello
. FLASK_APP="hello:create_app('dev')"
- The
create_app
factory inhello
is called with the string'dev'
as the argument.
If FLASK_APP
is not set, the command will look for a file called wsgi.py
or app.py
and try to detect an application instance or factory.
The environment in which the Flask app runs is set by the FLASK_ENV
environment variable. If not set it defaults to production
If the env is set to development
, the flask
command will enable debug mode and flask run
will enable the interactive debugger and reloader.
If you want to control debug mode separately, use FLASK_DEBUG
. The value 1
enables it, 0
disables it.
To explore the data in your application, you can start an interactive Python shell with the shell
command. An application context will be active, and the app instance will be imported.
$ flask shell
Python 3.6.2 (default, Jul 20 2017, 03:52:27)
[GCC 7.1.1 20170630] on linux
App: example
Instance: /home/user/Projects/hello/instance
>>>
Use shell_context_processor()
to add other automatic imports.
you can use Flask’s dotenv support to set environment variables automatically.
If python-dotenv is installed, running the flask
command will set environment variables defined in the files .env
and .flaskenv
.
This can be used to avoid having to set FLASK_APP
manually every time you open a new terminal.
Variables set on the command line are used over those set in .env
, which are used over those set in .flaskenv
.
.flaskenv
should be used for public variables, such as FLASK_APP
, while .env
should not be committed to your repository so that it can set private variables
The files are only loaded by the flask
command or calling run()
. If you would like to load these files when running in production, you should call load_dotenv()
manually.
Click is configured to load default values for command options from environment variables. The variables use the pattern FLASK_COMMAND_OPTION
. For example, to set the port for the run command, instead of flask run --port 8000
:
export FLASK_RUN_PORT=8000
flask run
* Running on http://127.0.0.1:8000/
These can be added to the .flaskenv
file just like FLASK_APP
to control default command options.
You can tell Flask not to load dotenv files even when python-dotenv is installed by setting the FLASK_SKIP_DOTENV
environment variable.
export FLASK_SKIP_DOTENV=1 flask run