Django get environment variables from apache
Question: I cannot seem to get Django to read the settings I configure from the
environment variables. I have followed some guides online, and found some
other questions, and as a result have tried configuring as below:

**Apache Config:**

    
    
    WSGIScriptAlias "/v4" /usr/local/myproject4/myproject4/wsgi.py
    WSGIPythonPath /usr/local/myproject4:/usr/local/myproject4/env/lib/python2.7/site-packages
    
    <VirtualHost *:8000>
    
            SetEnv MYPROJECT_SECRET_KEY 'xxx'
            SetEnv MYPROJECT_DB_USER 'xxxx'
            SetEnv MYPROJECT_DB_PASS 'xxxx'
    
            <Directory /usr/local/myproject4/myproject4>
                    <Files wsgi.py>
                            Order deny,allow
                            Allow from all
                    </Files>
            </Directory>
    
    
    </VirtualHost>
    

**My wsgi.py file looks contains this (to retrieve the settings):**

    
    
    import os
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject4.settings")
    
    from django.core.handlers.wsgi import WSGIHandler
    _application = WSGIHandler()
    
    def application(environ, start_response):
        for key, value in environ:
            if key.startswith('MYPROJECT_'):
                os.environ[key] = value;    
        return _application(environ, start_response)
    

**However whenever I try to retrieve the settings I get this:**

    
    
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] mod_wsgi (pid=21912): Target WSGI script '/usr/local/myproject4/myproject4/wsgi.py' cannot be loaded as Python module.
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] mod_wsgi (pid=21912): Exception occurred processing WSGI script '/usr/local/myproject4/myproject4/wsgi.py'.
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] Traceback (most recent call last):
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]   File "/usr/local/myproject4/myproject4/wsgi.py", line 14, in <module>
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]     from django.core.handlers.wsgi import WSGIHandler
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]   File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 11, in <module>
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]     from django.core.handlers import base
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]   File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 12, in <module>
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]     from django.db import connections, transaction
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]   File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/db/__init__.py", line 83, in <module>
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]     signals.request_started.connect(reset_queries)
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]   File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 88, in connect
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]     if settings.DEBUG:
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]   File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]     self._setup(name)
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]   File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/conf/__init__.py", line 49, in _setup
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]     self._wrapped = Settings(settings_module)
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]   File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/conf/__init__.py", line 128, in __init__
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]     mod = importlib.import_module(self.SETTINGS_MODULE)
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]   File "/usr/local/myproject4/env/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]     __import__(name)
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]   File "/usr/local/myproject4/myproject4/settings.py", line 29, in <module>
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]     SECRET_KEY = get_env_variable('MYPROJECT_SECRET_KEY')
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]   File "/usr/local/myproject4/myproject4/settings.py", line 23, in get_env_variable
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx]     raise ImproperlyConfigured(error_msg)
    [Wed Nov 20 17:07:08 2013] [error] [client xxx.xxx.xxx.xxx] ImproperlyConfigured: Set the MYPROJECT_SECRET_KEY environment variable
    

Really appreciate if someone could help me identify what I am doing wrong.


Answer: I needed the same feature to deal with prod/dev environments... and found out
the following article: <http://drumcoder.co.uk/blog/2010/nov/12/apache-
environment-variables-and-mod_wsgi/>

I just tried it, and it worked at once. Pay attention to the Handler's name
that is prefixed with a underscode:

    
    
    _application = django.core.handlers.wsgi.WSGIHandler()
    

