Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I set environment variables for Django?

everyone!

Django 1.11 + PostgreSQL 9.6 + Gunicorn + Ubuntu 16.04 in AWS

I want to set environment variables for sensitive info.(django secret key, DB password...)

I studied many articles about setting ways.

But when I tried os.environ['env_name'],

  1. .bashrc: Not working

  2. .bash_profile: Not working

  3. .profile: Not working

  4. /etc/environment: Not working

  5. Gunicorn script file.(systemd): I set them in gunicorn systemd script. It work very well.

But because I want to use the environment variables in other program too, I set them among 1~5 configurations. I don't understand why 1~5 configurations didn't work. Is there scope or priority of setting environment variables?

EDIT:

I use Ubuntu 16.04 server. I can't restart terminal session.

I tried 'source .bashrc' and logout/login. But It didn't work.

Of cource, 'echo $some_env_var' is working, I say, django can't read.

like image 273
jayuloy Avatar asked Jun 22 '17 07:06

jayuloy


People also ask

Where are environment variables stored in Django?

Environment variables live in the memory, not on the disk.

How do you set environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

How do I set an environment variable in Python VENV?

If you want to set environment variables each time the venv is started, you can assign them inside the activation script. If you're running a PowerShell terminal, you should edit Activate. ps1 in <YOUR_ENV>/Scripts and add an extra line to set an environment variable as follows.


2 Answers

.bashrc will work for local development but not for a production environment. I just spent quite a bit of time looking for the answer to this and here's what worked for me:

1) Create a file somewhere on your server titled settings.ini. I did this in /etc/project/settings.ini

2) Add your config data to that file using the following format where the key could be an environmental variable and the value is a string. Note that you don't need to surround the value in quotes.

[section]
secret_key_a=somestringa
secret_key_b=somestringb

3) Access these variables using python's configparser library. The code below could be in your django project's settings file.

from configparser import RawConfigParser

config = RawConfigParser()
config.read('/etc/project/settings.ini')

DJANGO_SECRET = config.get('section', 'secret_key_a')

Source: https://code.djangoproject.com/wiki/SplitSettings (ini-style section)

like image 70
Braden Holt Avatar answered Sep 22 '22 04:09

Braden Holt


The simplest solution is as already mentioned using os.environ.get and then set your server environment variables in some way (config stores, bash files, etc.)

Another slightly more sophisticated way is to use python-decouple and .env files. Here's a quick how-to:

1) Install python-decouple (preferably in a venv if not using Docker):

pip install python-decouple

2) Create a .env file in the root of your Django-project, add a key like;

SECRET_KEY=SomeSecretKeyHere

3) In your settings.py, or any other file where you want to use the configuration values:

from decouple import config

...

SECRET_KEY = config('SECRET_KEY')

4) As you probably don't want these secrets to end up in your version control system, add the file to your .gitignore. To make it easier to setup a new project, you could have a .env_default checked into the VCS containing default/dummy-values that's not used in production.

like image 26
Andreas Bergström Avatar answered Sep 19 '22 04:09

Andreas Bergström