title: Python app import error in Django with WSGI gunicorn
body: <p>I'm trying to deploy a Django app with gunicorn on Heroku and I've run into a few hitches.</p>
  
  <p>When I began my project my Django version was 1.3 and didn't contain the standard wsgi.py module, so I added the standard wsgi module as top/wsgi.py (top being my project name, turk being my app name, topturk being the containing directory - preserved so error logs make sense below).</p>
  
  <p>Now when I run</p>
  
  <pre><code>gunicorn top.wsgi:application -b 0.0.0.0:$PORT
  </code></pre>
  
  <p>The server successfully starts up,</p>
  
  <pre><code>19:00:42 web.1     | started with pid 7869
  19:00:42 web.1     | 2012-07-25 19:00:42 [7869] [INFO] Starting gunicorn 0.14.5
  19:00:42 web.1     | 2012-07-25 19:00:42 [7869] [INFO] Listening at: http://0.0.0.0:5000 (7869)
  19:00:42 web.1     | 2012-07-25 19:00:42 [7869] [INFO] Using worker: sync
  19:00:42 web.1     | 2012-07-25 19:00:42 [7870] [INFO] Booting worker with pid: 7870
  </code></pre>
  
  <p>but then when I navigate to 0.0.0.0:5000 I get returned an Internal Server Error:</p>
  
  <pre><code>19:00:45 web.1     | 2012-07-25 17:00:45 [7870] [ERROR] Error handling request
  19:00:45 web.1     | Traceback (most recent call last):
  19:00:45 web.1     |   File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 102, in handle_request
  19:00:45 web.1     |     respiter = self.wsgi(environ, resp.start_response)
  19:00:45 web.1     |   File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
  19:00:45 web.1     |     self.load_middleware()
  19:00:45 web.1     |   File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
  19:00:45 web.1     |     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
  19:00:45 web.1     | ImproperlyConfigured: Error importing middleware turk.middleware.subdomain: "No module named turk.middleware.subdomain"
  19:00:47 web.1     | 2012-07-25 17:00:47 [7870] [ERROR] Error handling request
  19:00:47 web.1     | Traceback (most recent call last):
  19:00:47 web.1     |   File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 102, in handle_request
  19:00:47 web.1     |     respiter = self.wsgi(environ, resp.start_response)
  19:00:47 web.1     |   File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
  19:00:47 web.1     |     self.load_middleware()
  19:00:47 web.1     |   File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
  19:00:47 web.1     |     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
  19:00:47 web.1     | ImproperlyConfigured: Error importing middleware turk.middleware.subdomain: "No module named turk.middleware.subdomain"
  </code></pre>
  
  <p>I'm assuming this is a python path error, where the server doesn't know how to import from my app directory</p>
  
  <p>The relevant import code is here in settings:</p>
  
  <pre><code>MIDDLEWARE_CLASSES = (
      'turk.middleware.subdomain.SubdomainMiddleware',
      'turk.middleware.removewww.RemoveWWWMiddleware',
  )
  </code></pre>
  
  <p>I attempted to fix this problem by inserting my app directory into sys.path like so at the top of my settings.py file like so:</p>
  
  <pre><code>PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
  sys.path.insert(1, PROJECT_ROOT+'/turk/')
  </code></pre>
  
  <p>Which I've verified adds the app directory to the path, but still no dice. Any ideas? Also</p>
  
  <pre><code>sys.path.insert(1, PROJECT_ROOT+'/turk/')
  </code></pre>
  
  <p>seems hackish and adds at least two copies of the directory to the path, what's the correct way to append to PYTHON_PATH in Django? Thanks!</p>