Forums

using .ENV var in a console

I encounter a small problem when working with .env var. I'm setting up a Flask app in a virtual env. Working with .env var in the web app is ok. Accessing those var in the console works also (with echo $SECRET) but running the file from the console gives me 'none' when I try to print(os.getenv('SECRET')). What am I doing wrong?

The Run button does not activate the virtualenv, so the .env is not applied automatically. So if you want to use it like that, use

from dotenv import load_dotenv
load_dotenv(dotenv_path=env_path)

where env_path is the full path to the .env file you want to load.

Great, that did the job! note : I was not using the run-button, but running the file from the command-line. Now it works from the command-line. Not from the run-button. But is ok for me. many thx for the fast response.