Safeguarding Secrets and Environment Variables in Python | NodeFlux.eu Docs
Keep passwords, database URIs, and API tokens out of Python source files using os.environ and python-dotenv.
Steps
- Import os module in your Python entry script.
- Read configuration values using os.environ.get("KEYNAME", "defaultvalue").
- For local testing, use python-dotenv package to load values from a local .env file.
- Ensure .env is added to your .gitignore file so secrets are never pushed to public code repositories.
- On NodeFlux, enter all secrets directly into the service Environment Variables settings panel.
- Restart service and verify values are read without syntax errors.
Notes
- Using os.environ.get() prevents KeyError exceptions if an environment variable is missing.
- Never print or log raw os.environ dictionaries in application log files.
Related NodeFlux pages
- Running Scheduled Tasks with APScheduler and Celery — Execute periodic cron jobs, background cleanup scripts, and scheduled events in Python apps.
- Optimizing Python Memory Usage and Garbage Collection — Reduce Python RAM footprints on shared host containers by optimizing data structures and garbage collection.
- Writing Non-Blocking Concurrency with Python asyncio — Master asynchronous Python event loops, avoid blocking code traps, and handle concurrent tasks.