I'm using python-dotenv to set up environment variables and I got everything to work. I can now call my environment variables from settings.py
that I defined in .env
. That's cool and all but is there a way to call the environment variables from other files in subdirectories? Note that I'm not using django or any framework.
# settings.py
import os
from dotenv import load_dotenv
BASEDIR = os.path.abspath(os.path.dirname(__file__))
load_dotenv(os.path.join(BASEDIR, '.env'))
CLIENT_ID = os.getenv("CLIENT_ID")
# This works!
print(CLIENT_ID)
# other_file.py
import os
CLIENT_ID = os.getenv("CLIENT_ID")
# This does not :(, prints None
print(CLIENT_ID)
I'm sure I can load .env
in every file where env variables are needed, but is there a more efficient way?