Create environment variables in Flask .flaskenv
Environment variables are an important part of the application since in order to use certain tools such as Flask Migrate, the Shell or the Flask run command, we must establish the environment variable called FLASK_APP:
$ export FLASK_APP=my_app
On Windows:
$ set FLASK_APP=my_app
We have seen how to establish it through the terminal as we showed in chapter 5, in which it was required to be able to execute the Flask Migrate commands. But we haven't seen all the potential this offers us and how Flask uses it. For example, we can use files to manage these environment variables, as with technologies such as CodeIgniter or Laravel, we can create these files to manage the project's environment variables:
.env
And in the case of Flask, also the:
.envflask
To manage these environment variables.
In this chapter we are going to learn how to use environment variables through files.
We are going to create a new application as we talked about in chapter 1.
If we run:
$ flask run
We will see an error like the following:
Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
In which it tells us that the environment variable called FLASK_APP must be established, indicating the name of the application module in Flask.
To do this, we can do it through the terminal, on Mac or Linux:
$ export FLASK_APP=my_app
On Windows:
$ set FLASK_APP=my_app
Or set it to file:
.flaskenv
FLASK_APP=my_app
The value of FLASK_APP corresponds to the name of the module, which in this example is named my_app:
my_app/__init__.py
config.py
.env
.flaskenv
run.py
Where the application startup file is located:
my_app/__init__.py
Now, if we try to execute the command:
$ flask run
We will see that it still does not work, it is because Flask is not recognizing the file generated previously; in order for it to be recognized as a valid entry to set the environment variables, we must install an additional package:
$ pip install python-dotenv
If we run again:
$ flask run
We will see that now if the server starts correctly; If we try to execute other commands that require knowing the application instance, the variable called app found inside my_app/__init__.py.
The fundamental advantage of using files to manage environment variables is that we do not have to set the environment variables of our application every time we open a terminal, since they are taken from the file.
These are the main options that we can configure:
- FLASK_ENV - Indicates the environment, by default it is production.
- FLASK_DEBUG - Indicates if the application is in DEBUG mode, default is TRUE.
- FLASK_APP - Used to specify the application module.
- FLASK_RUN_PORT - To specify the port when starting the application.
You can learn about other Flask environment variables at:
https://flask.palletsprojects.com/en/latest/config/
We have two dot files: .env and .flaskenv. We use .flaskenv for any Flask CLI configuration command and use .env for our application configuration.
For example, we specify the name of the module, as we did above:
.flaskenv
FLASK_APP=my_app
And in this file any other configuration that does not have the FLASK_* prefix:
.env
SECRET_KEY=12345
API_KEY=123
We can specify other settings such as:
.flaskenv
FLASK_ENV=development
FLASK_RUN_PORT=8080
To access the environment variables from the project, we must use the environ() function:
config.py
from os import environ
SECRET_KEY = environ.get('SECRET_KEY')
API_KEY = environ.get('API_KEY')
FLASK_APP = environ.get('FLASK_APP')
The main advantage of using files to manage environment variables is that we can have all the project's environment variables in one place, we can have multiple configurations in other .env files and change from one to another easily and have files to manage environment variables for development, production, testing, among others operations.
- Andrés Cruz
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter