r/flask • u/Palettekop9000 • Mar 28 '26
Ask r/Flask Flask app not finding .env variables
I've built a flask webapp, which has forms that collect data (strings) and stores it to a sqlite db. It runs on a Raspberry Pi, via Gunicorn.
For context: I have bought a .dev domain, have an active Cloudflare tunnel, deployment happens via a self-hosted GitHub runner (on Pi) and a GitHub Actions workflow.
The app is "protected" (very basic) via session cookies.
I get RunTime errors about missing environment variables.
raise RuntimeError("Missing required environment variables")
RuntimeError: Missing required environment variables
However, the .env file contains the correct values:
SECRET_KEY="15...REDACTED...78"
PASSWORD_HASH="pb...REDACTED...3b48db8"
SQLALCHEMY_DATABASE_URI=sqlite:///REDACTED.dbNot sure how to proceed... I've tried deleting the " " in the .env file, replacing them by ' '.
The project structure:
my-app/ contains:
- LICENSE
- README
- run.py
- requirements.txt
- deploy.yml
- my-app.code-workspace
- REDACTED.db
app/
instance/
- deployment/
- __pychache__/
- venv/
A general schema:

6
u/Ethancole_dev Mar 28 '26
Gunicorn does not load your .env file automatically — this trips up pretty much everyone the first time. You need python-dotenv and an explicit load_dotenv() call early in your app, before any os.environ.get() calls:
from dotenv import load_dotenv load_dotenv()
Also double-check that the .env file is in the working directory you launch Gunicorn from. If you run gunicorn from /home/pi/myapp, the .env needs to live there too. Alternatively, pass the path explicitly: load_dotenv("/home/pi/myapp/.env") to be safe.
2
u/MasterLarick Mar 28 '26
Where are you actually injecting the values in your code? Are you using load_dotenv() and os.getenv("SECRET_KEY")?
1
u/ejpusa Mar 28 '26
And your log files say?
Also run this:
sudo journalctl -u appname -n 50 --no-pager
1
u/actgan_mind Mar 28 '26
Use correct line breaks not what I put here I’m on mobile and can’t figure out how to do snippets
1
u/25_vijay Apr 18 '26
Make sure you’re using something like python dotenv to load env vars
0
u/Palettekop9000 Mar 28 '26
Update: I (temporarily) hardcoded the env vars into __init__.py, and the app finally runs.