Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?
like image 922
rpivovar Avatar asked Aug 01 '18 01:08

rpivovar


People also ask

What is Django env?

django-environ is the Python package that allows you to use Twelve-factor methodology to configure your Django application with environment variables.

What is env file Python?

env file is a text file containing key value pairs of all the environment variables required by your application. This file is included with your project locally but not saved to source control so that you aren't putting potentially sensitive information at risk.

What is the purpose of .env file?

The . env file contains the individual user environment variables that override the variables set in the /etc/environment file. You can customize your environment variables as desired by modifying your . env file.


1 Answers

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.

like image 162
Paulo Scardine Avatar answered Oct 28 '22 01:10

Paulo Scardine