.flaskenv or .env file not being read
Question:
I have a flask app that uses some enviroment variables, I don’t now what has changed but now it doesn’t read the variables in the .flaskenv file, already tried changing its name to .env, still not working.
This is the .flaskenv file:
FLASK_APP=app
FLASK_ENV=development
CONSUMER_KEY=
CONSUMER_SECRET=
ACCESS_KEY=
ACCESS_SECRET=
SEC_USERNAME=
SEC_PASSWORD=
In the app.py file:
from flask import Flask, render_template, url_for, request, redirect, session, g
import os
s_user = os.environ.get('SEC_USERNAME')
s_pass = os.environ.get('SEC_PASSWORD')
print(s_user)
print(s_pass)
if __name__ == "__main__":
app.run()
It prints “none”.
If i do flask run :
Usage: flask run [OPTIONS]
Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.
For more information see http://flask.pocoo.org/docs/latest/quickstart/
pip list:
(venv) λ pip list
Package Version
----------------- ----------
certifi 2020.4.5.2
chardet 3.0.4
click 6.7
Flask 0.12.2
idna 2.9
itsdangerous 0.24
Jinja2 2.10
MarkupSafe 1.0
oauthlib 3.1.0
pip 19.2.3
PySocks 1.7.1
python-dotenv 0.13.0
requests 2.23.0
requests-oauthlib 1.3.0
setuptools 41.2.0
six 1.15.0
tweepy 3.8.0
urllib3 1.25.9
Werkzeug 0.13
Answers:
As written in the flask docs:
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, and to set configuration using environment variables similar to how some deployment services work.
make sure to run pip install python-dotenv
Edit
This flask seems to be very old, try updating it with
pip install -U Flask
I have a flask app that uses some enviroment variables, I don’t now what has changed but now it doesn’t read the variables in the .flaskenv file, already tried changing its name to .env, still not working.
This is the .flaskenv file:
FLASK_APP=app
FLASK_ENV=development
CONSUMER_KEY=
CONSUMER_SECRET=
ACCESS_KEY=
ACCESS_SECRET=
SEC_USERNAME=
SEC_PASSWORD=
In the app.py file:
from flask import Flask, render_template, url_for, request, redirect, session, g
import os
s_user = os.environ.get('SEC_USERNAME')
s_pass = os.environ.get('SEC_PASSWORD')
print(s_user)
print(s_pass)
if __name__ == "__main__":
app.run()
It prints “none”.
If i do flask run :
Usage: flask run [OPTIONS]
Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.
For more information see http://flask.pocoo.org/docs/latest/quickstart/
pip list:
(venv) λ pip list
Package Version
----------------- ----------
certifi 2020.4.5.2
chardet 3.0.4
click 6.7
Flask 0.12.2
idna 2.9
itsdangerous 0.24
Jinja2 2.10
MarkupSafe 1.0
oauthlib 3.1.0
pip 19.2.3
PySocks 1.7.1
python-dotenv 0.13.0
requests 2.23.0
requests-oauthlib 1.3.0
setuptools 41.2.0
six 1.15.0
tweepy 3.8.0
urllib3 1.25.9
Werkzeug 0.13
As written in the flask docs:
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, and to set configuration using environment variables similar to how some deployment services work.
make sure to run pip install python-dotenv
Edit
This flask seems to be very old, try updating it with
pip install -U Flask