I have been using python-dotenv successfully in my python app. I am now starting to refactor and creating packages to hold some of my support files. When i move the .env file into the package folder my env variables are not set.
As an example
The following structure works.
/
- app.py
- .env
#app.py
import os
from dotenv import load_dotenv
load_dotenv()
print(os.getenv("DB_PASSWORD"))
Result: kdfhsffdfjd
The following structure does not work
/
- database
- __init__.py
- connection.py
- .env
- app.py
#connection.py
import os
from dotenv import load_dotenv
load_dotenv()
print(os.getenv("DB_PASSWORD"))
Result: None
# app.py
from database import connection
You need to actually call
load_dotenv
function. You can pass the .env file path as an argument or you could try to usefind_dotenv
method: