Skip to content

Instantly share code, notes, and snippets.

@gauravvjn
Last active September 6, 2018 08:06
Show Gist options
  • Save gauravvjn/4a6e5c1e1ded0759779674f308caaa23 to your computer and use it in GitHub Desktop.
Save gauravvjn/4a6e5c1e1ded0759779674f308caaa23 to your computer and use it in GitHub Desktop.
Dynamic env variables load from .env file while executing manage.py <command>
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.settings.base")
from django.core.management import execute_from_command_line
# ---------- 5 lines ------------
with open('.env') as f:
for e in f.readlines():
if e.strip():
k, v = list(map(lambda x: x.strip(), e.split('=', 1)))
os.environ.setdefault(k, v)
execute_from_command_line(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment