12

I'm pretty new to Django / Python, and I'm trying to figure out how a .env file relates to a Django project.

Example .env:

DATABASE_URL=postgres://postgres_user@db:xxxx/postgres_db
DJANGO_SETTINGS_MODULE=spare.settings.dev
SECRET_KEY=example

I did manage to find this Stack Overflow post, which gives some information, but was hoping for a bit more.

  • Do all Django projects have a .env file?
  • Do non-Django python projects have a .env file, or is it generally a Django-related thing?
  • Where is the .env file typically being called from? In other words, how does the rest of the project know that the .env file exists?
jnns
  • 5,148
  • 4
  • 47
  • 74
rpivovar
  • 3,150
  • 13
  • 41
  • 79
  • `.env` file is very very simple ianswered here [https://stackoverflow.com/a/63446561/10620360](https://stackoverflow.com/a/63446561/10620360) – R.A.M Aug 17 '20 at 08:12

1 Answers1

11

We can only guess because we don't have access to your actual environment.

The .env file may be a container manager thing or something from libraries like python-decouple - for practical effects the .env will be used to populate the environment variables when the container "boots" or will be used to fill instance settings.

There is a common pattern made popular by the Twelve-Factor app: the item III is "Store config in the environment". Then in the settings.py file you use the KEY = os.environ.get('KEY', 'defaul_value'). The idea is to separate instance settings from project settings from code.

Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • Oh right. Docker is involved, which I also don't know much about. That helps. Thanks. I'll mark this correct when I can. – rpivovar Aug 01 '18 at 01:54
  • https://stackoverflow.com/questions/43416029/creating-env-file-on-the-root-of-the-project?noredirect=1&lq=1 – CodingNow Aug 08 '18 at 16:17