4

I am working with django and virtualenvwrapper. My objective is to remove all sensitive information from the settings file per the 12Factor app suggestions (http://12factor.net) and ultimately deploy to heroku. When testing this locally, to achieve this, I have created a .env file with different variable values like SECRET_KEY. I went to my virtualenv directory and added the following line to the postactivate script:

source .env 

Whenever I start my virtual env for a project aka workon project_name, the environment variables from .env are available if I echo from the terminal

$ echo $SECRET_KEY
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

However when I try to access those variables from python they are unavailable

>>> import os
>>> os.environ.get('SECRET_KEY')
>>> 

What is the correct way for python to access the environment variables stored in a .env file?

.env file:

WEB_CONCURRENCY=2
SECRET_KEY='XXXXXXXXXXXX'
DEBUG=True
alexolivas
  • 63
  • 1
  • 6

1 Answers1

1

I think your problem is that you are defining it in your current shell by doing SECRET_KEY=xxxxxxx, but when you open up a python shell, it's running in a sub process and you lost the environment variable in that shell. export will make the variable available in sub process as well.

You should have:

export SECRET_KEY=xxxxxxxx

In your .env file to make it work.

Edit:

From what I read from your links, that's just a normal linux shell environment variable. But django needs to have SECRET_KEY as a python constant in the settings. Linux environment variables and python variables are two different things, so defining a env variable SECRET_KEY doesn't let django recognize settings.SECRET_KEY. You should still consider using separate settings file, which is mostly recommended.

Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • I've been following heroku's django deployment guide and they are using an env file for this, the example is a bit incomplete which is where I'm stuck. Plus I think its cleaner to maintain 1 settings files even if they all inherit from the same base_settings for all the common settings. Using export worked but now I'm more confused than when I started since you're suggesting that's not good practice. – alexolivas Jan 29 '16 at 20:44
  • Oh, I don't know enough about heroku, maybe I was wrong. I will remove my suggestion about settings file because heroku might have some trick to make it work with `.env` file. But I think you should understand why you need `export`. In case you still want to use multiple settings file, check this: http://stackoverflow.com/questions/24071489/django-using-multiple-settings-files-with-heroku – Shang Wang Jan 29 '16 at 20:49
  • BTW, I'm still interested in where did you find the guide though. – Shang Wang Jan 29 '16 at 20:55
  • That makes two of us, I just started learning about heroku a couple days ago. I was using multiple settings files when my app was hosted at webfaction. This tutorial is for [django on heroku](https://devcenter.heroku.com/articles/django-app-configuration) and this one is for the [local config vars](https://devcenter.heroku.com/articles/heroku-local) that heroku uses and how to run your code in development. – alexolivas Jan 29 '16 at 20:59
  • Also, I do understand the need for export. Thanks! – alexolivas Jan 29 '16 at 21:01
  • I edited my answer. I don't think the guide for django setup mentions anything related to environment variable, so I guess heroku doesn't have anything different than a normal linux machine. – Shang Wang Jan 29 '16 at 21:11