I have abstracted the reading of the .env
file to settings.py
but I'm not sure if this is idiomatic to python:
# .env
SECRET=XXXX
# settings.py
import os
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
SECRET = os.environ.get("SECRET_KEY")
# app.py
from ..settings import CONSUMER_KEY
def useSecret(SECRET):
Should each module read the .env
file directly?
This, in my opinion, is a case for using a singleton (which is slightly awkward in Python) but nonetheless you may find this useful.
If you care to run this code as is (assuming you have a .env file with a SECRET_KEY key) then you'll see that load_dotenv is only called once