Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.flaskenv or .env file not being read

Tags:

python

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

like image 979
Santiago García Avatar asked Jun 16 '20 15:06

Santiago García


People also ask

How do I use .env files in Flask?

env file inside the Python Flask application. Reads the key-value pair from the . env file and adds them to the environment variable. It is great for managing app settings during development and in production using 12-factor principles.

Does .env file work in Windows?

env is loaded exactly. Go doesn't handle . env natively, you're probably using godotenv. It sets the env variables for the current process (if using it as a library) and might work on windows.

What does the flask_env variable do?

Similarly, the FLASK_ENV variable sets the environment on which we want our flask application to run. For example, if we put FLASK_ENV=development the environment will be switched to development. By default, the environment is set to development. Now let us look at some example so that one can easily understand “what happens in reality”!

Is flaskenv OS specific?

The .flaskenv isn't OS specific. It's loaded with the dotenv package, not anything related to the OS. And for the debug, ya you're right.

Could not locate a flask application?

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. This is a common error though. Normally, to fix this, we would export the environment variable FLASK_APP to be equal to the name of our app directory like this:

Why can't I debug flaskenv in production?

The .flaskenv isn't OS specific. It's loaded with the dotenv package, not anything related to the OS. And for the debug, ya you're right. It's a side effect of FLASK_ENV defaulting to production, which forces debug to false.


Video Answer


1 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

like image 144
Reznik Avatar answered Nov 15 '22 04:11

Reznik