1

I am just deploying my django application and when I try to access it the application throws the following Error:

Traceback:

  ...
  File "/home/sysadmin/public_html/aegee-stuttgart.org/aegeewww/settings.py", line 23, in <module>
    SECRET_KEY = os.environ['SECRET_KEY']
  File "/usr/lib/python2.7/UserDict.py", line 40, in __getitem__
    raise KeyError(key)
KeyError: 'SECRET_KEY'

The Key is being exportet with .bashrc and when I try to access it in console with

import os
print(os.environ['SECRET_KEY'])

I get the right key.

As of this thread, it is not possible to make env vars accessible to django with .bashrc.

But how do I do it then?

Edit: wsgi.py:

import os, sys

# add the aegeewww project path into the sys.path
sys.path.append('/home/sysadmin/public_html/aegee-stuttgart.org/aegeewww')

# add the virtualenv site-packages path to the sys.path
sys.path.append('/home/sysadmin/.virtualenvs/django/lib/python2.7site-packages')

# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aegeewww.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

I have managed It with your help! Thank you all a lot!

For future references:

At first set the Keys in your apache configuration like this:

<VirtualHost *:80>
    ...
    SetEnv KEY_TO_STORE keyvalue
    ...
</VirtualHost>

I changed my wsgi.py as suggested in this thread by using the WSGIEnvironment class.

In there you have to add

os.environ['KEY_TO_STORE'] = environ['KEY_TO_STORE']

to make the wsgi environ variables accessible with os.environment in settings.py

my updated wsgy.py:

import os, sys

# add the aegeewww project path into the sys.path
sys.path.append('/home/sysadmin/public_html/aegee-stuttgart.org/aegeewww')

# add the virtualenv site-packages path to the sys.path
sys.path.append('/home/sysadmin/.virtualenvs/django/lib/python2.7/site-packages')

import django
from django.core.handlers.wsgi import WSGIHandler

class WSGIEnvironment(WSGIHandler):

    def __call__(self, environ, start_response):
        # let django recognize env variables
        os.environ['SECRET_KEY'] = environ['SECRET_KEY']

        # poiting to the project settings
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aegeewww.settings")

        django.setup()
        return super(WSGIEnvironment, self).__call__(environ, start_response)

application = WSGIEnvironment()
Community
  • 1
  • 1
setchock
  • 151
  • 1
  • 2
  • 9
  • It should be possible (be it in Django or not) to access the environment variable. The thread you linked simple states that Django deployed inside Apache cannot access them because the `.bashrc` mentioned there was a local file in the developer's laptop. Are you running locally or in a server? – David Gomes Jul 31 '16 at 14:52
  • 2
    How is Django deployed? WSGI? How is the WSGI server set up? – Martijn Pieters Jul 31 '16 at 14:52
  • I also found [this](http://andrewtorkbaker.com/using-environment-variables-with-django-settings) cool article you might want to take a look at. – David Gomes Jul 31 '16 at 14:52
  • Django is deployed on a server with mod_wsgi, checking out the link now. Should I post the wsgi configuration? – setchock Jul 31 '16 at 14:55
  • 1
    Sounds a lot like [Django + mod\_wsgi. Set OS environment variable from Apache's SetEnv](http://stackoverflow.com/q/26979579) then. – Martijn Pieters Jul 31 '16 at 15:00
  • You should check this out http://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python – Rohit Khatri Jul 31 '16 at 15:08
  • @MartijnPieters Thanks for that, So with django > 1.6 it is not possible to access the environment variables directly in settings.py? why is it in the docs then? – setchock Jul 31 '16 at 15:10
  • 2
    @setchock: it is perfectly possible to access env variables. But `mod_wsgi` doesn't pass them on from the parent environment, it puts these in the standard WSGI parameter `environ` instead. – Martijn Pieters Jul 31 '16 at 15:11
  • @RohitKhatri: and what has that got to do with a `mod_wsgi` setup? – Martijn Pieters Jul 31 '16 at 15:12
  • @RohitKhatri I do not want to set env variables in python, I want to access them. – setchock Jul 31 '16 at 15:12
  • @setchock Your heading says how store instead how to retrieve. – Rohit Khatri Jul 31 '16 at 15:13
  • Try this http://stackoverflow.com/questions/4906977/access-environment-variables-from-python – Rohit Khatri Jul 31 '16 at 15:14
  • @RohitKhatri Please read my post, I am doing exactly that, but it is not working. I will try out the approach Martijn linked to, lets see if that works – setchock Jul 31 '16 at 15:16
  • @setchock sure try that out, seems like a solution. – Rohit Khatri Jul 31 '16 at 15:17
  • @MartijnPieters I have tried what is said in your link, but now i only get the same error early.I have updated my question accordingly – setchock Jul 31 '16 at 15:40
  • @MartijnPieters No, I did just forget to add the Variable withSetEnv, I think I got it now, thanks! – setchock Jul 31 '16 at 15:46
  • 1
    Setting process wide environment variables on a per request basis from values passed in the per request WSGI environ is regarded as bad practice. This can create some strange problems. You are better off setting them via the WSGI script file in some way at global scope, that is only once when the file is loaded. If you need an indicator as to what environment you are in passed from the Apache configuration at time script loaded, use daemon mode and a named process group. You can trigger on the process group name, available via ``import mod_wsgi; mod_wsgi.process_group``. – Graham Dumpleton Jul 31 '16 at 22:39

0 Answers0