You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#!/usr/bin/env pythonimportosimportsysimportdotenvif__name__=="__main__":
dotenv.read_dotenv()
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
try:
fromdjango.core.managementimportexecute_from_command_lineexceptImportErrorasexc:
raiseImportError(
"Couldn't import Django. Are you sure it's installed and ""available on your PYTHONPATH environment variable? Did you ""forget to activate a virtual environment?"
) fromexcexecute_from_command_line(sys.argv)
You can also pass read_dotenv() an explicit path to the .env
file, or to the directory where it lives. It's smart, it'll figure it
out.
By default, variables that are already defined in the environment take
precedence over those in your .env file. To change this, call
read_dotenv(override=True).
Check out
tests.py
to see all the supported formats that your .env can have.
Using with WSGI
If you're running Django with WSGI and want to load a .env file,
your wsgi.py would look like this:
That's it. Now go 12 factor the crap out of something.
Common problems
AttributeError: module 'dotenv' has no attribute 'read_dotenv'
There is another similar package,
python-dotenv, which also
contains a module called dotenv. If that package is installed, then you
will see:
AttributeError: module 'dotenv' has no attribute 'read_dotenv'
To resolve this, uninstall python-dotenv.
read_dotenv is not reading from my environment file!
By default, variables that are already defined in the environment take
precedence over those in your .env file. To change this, call
read_dotenv(override=True).