title: DJANGO_SETTINGS_MODULE is undefined?
body: <pre><code>import os
  from os.path import abspath, dirname
  import sys
  
  # Set up django
  project_dir = abspath(dirname(dirname(__file__)))
  sys.path.insert(0, project_dir)
  os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
  </code></pre>
  
  <p>I did that, and I still get </p>
  
  <pre><code>Traceback (most recent call last):
    File "main.py", line 4, in &lt;module&gt;
      from django.middleware.csrf import get_token
    File "/var/lib/system-webclient/system-webenv/lib/python2.6/site-packages/django/middleware/csrf.py", line 14, in &lt;module&gt;
      from django.utils.cache import patch_vary_headers
    File "/var/lib/system-webclient/system-webenv/lib/python2.6/site-packages/django/utils/cache.py", line 24, in &lt;module&gt;
      from django.core.cache import cache
    File "/var/lib/system-webclient/system-webenv/lib/python2.6/site-packages/django/core/cache/__init__.py", line 68, in &lt;module&gt;
      cache = get_cache(settings.CACHE_BACKEND)
    File "/var/lib/system-webclient/system-webenv/lib/python2.6/site-packages/django/utils/functional.py", line 276, in __getattr__
      self._setup()
    File "/var/lib/system-webclient/system-webenv/lib/python2.6/site-packages/django/conf/__init__.py", line 38, in _setup
      raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
  ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined
  </code></pre>
  
  <p>It was a small script calling some django functionality, but it is telling me ENVIRONMENT VARIABLE issue. </p>
  
  <p>What can I do? I spent my whole afternoon reading many posts....</p>
  
  <p>Thanks for helping! This was my 1st time hitting it.</p>
  
  <hr>
  
  <p>The traceback. It doesn't give raise our manual exception. It is giving the same exception as above.</p>
  
  <p><em>webclient/main.py</em></p>
  
  <pre><code>import os
  from os.path import abspath, dirname
  import sys
  path = '/var/lib/graphyte-webclient/webclient'
  if path not in sys.path:
      sys.path.append(path)
  sys.path.insert(0, path)
  os.environ["DJANGO_SETTINGS_MODULE"] = "webclient.settings"
  raise Exception("DJANGO_SETTINGS_MODULE = " + str(os.environ["DJANGO_SETTINGS_MODULE"]))
  
      .... other things
  </code></pre>
  
  <p>In <em>manage.py shell</em></p>
  
  <pre><code>&gt;&gt;&gt; from webclient.settings import settings
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
  ImportError: cannot import name settings
  
  &gt;&gt;&gt; import webclient
  &gt;&gt;&gt; webclient
  &lt;module 'webclient' from '/var/lib/system-webclient/webclient/../webclient/__init__.pyc'&gt;
  
  &gt;&gt;&gt; import webclient.settings
  &gt;&gt;&gt; webclient.settings
  &lt;module 'webclient.settings' from '/var/lib/system-webclient/webclient/../webclient/settings.pyc'&gt;
  </code></pre>
  
  <p><strong>webclient/deploy/pinax.fcgi</strong></p>
  
  <pre><code>import os
  import sys
  
  from os.path import abspath, dirname, join
  from site import addsitedir
  
  sys.path.insert(0, abspath(join(dirname(__file__), "../../")))
  
  from django.conf import settings
  os.environ["DJANGO_SETTINGS_MODULE"] = "webclient.settings"
  
  sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))
  
  from django.core.servers.fastcgi import runfastcgi
  runfastcgi(method="threaded", daemonize="false")
  </code></pre>
  
  <p><strong>webclient/deploy/pinax.wcgi</strong></p>
  
  <pre><code>import os
  import sys
  
  from os.path import abspath, dirname, join
  from site import addsitedir
  
  sys.path.insert(0, abspath(join(dirname(__file__), "../../")))
  
  from django.conf import settings
  os.environ["DJANGO_SETTINGS_MODULE"] = "webclient.settings"
  #os.environ["SCRIPT_NAME"] = "/natrium"
  
  sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))
  
  from django.core.handlers.wsgi import WSGIHandler
  application = WSGIHandler()
  </code></pre>
  
  <p><a href="http://i.imgur.com/ROj8h.jpg" rel="nofollow">filesystem image</a></p>