How does a .env file relate to Python / Django?

14,669

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.

Share:
14,669
rpivovar
Author by

rpivovar

Software Engineer based in New York.

Updated on June 05, 2022

Comments

  • rpivovar
    rpivovar almost 2 years

    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?
  • rpivovar
    rpivovar almost 6 years
    Oh right. Docker is involved, which I also don't know much about. That helps. Thanks. I'll mark this correct when I can.
  • CodingNow
    CodingNow over 5 years