Gunicorn, no module named 'myproject
<p>I'm installing a previously built website on a new server. I'm not the original developer.</p>

<p>I've used Gunicorn + nginx in the past to keep the app alive (basically following <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04" rel="nofollow">this tutorial</a>), but am having problems with it here.</p>

<p>I <code>source venv/bin/activate</code>, then <code>./manage.py runserver 0.0.0.0:8000</code> works well and everything is running as expected. I shut it down and run <code>gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application</code>, and get the following:</p>

<pre><code>[2016-09-13 01:11:47 +0000] [15259] [INFO] Starting gunicorn 19.6.0
[2016-09-13 01:11:47 +0000] [15259] [INFO] Listening at: http://0.0.0.0:8000 (15259)
[2016-09-13 01:11:47 +0000] [15259] [INFO] Using worker: sync
[2016-09-13 01:11:47 +0000] [15262] [INFO] Booting worker with pid: 15262
[2016-09-13 01:11:47 +0000] [15262] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
    worker.init_process()
  File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/workers/base.py", line 126, in init_process
    self.load_wsgi()
  File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/var/www/myproject/venv/lib/python3.5/site-packages/gunicorn/util.py", line 357, in import_app
    __import__(module)
ImportError: No module named 'myproject.wsgi'
[2016-09-13 01:11:47 +0000] [15262] [INFO] Worker exiting (pid: 15262)
[2016-09-13 01:11:47 +0000] [15259] [INFO] Shutting down: Master
[2016-09-13 01:11:47 +0000] [15259] [INFO] Reason: Worker failed to boot.
</code></pre>

<p>I believe it something to do with the structure of the whole application. Before, I've built apps with the basic structure of:</p>

<pre><code>myproject
âââ manage.py
âââ myproject
âÂ Â  âââ urls.py
âÂ Â  âââ views.py
âÂ Â  âââ component1
â   âÂ Â  âââ urls.py
â   âÂ Â  âââ views.py
âÂ Â  âââ component2
â   âÂ Â  âââ urls.py
â   âÂ Â  âââ views.py
âââ venv
âÂ Â  âââ bin
âÂ Â  âââ ...
</code></pre>

<p>This one, instead, has a structure like:</p>

<pre><code>myproject
âââ apps
âÂ Â  âââ blog
â   âÂ Â  âââ urls.py
â   âÂ Â  âââ views.py
âÂ Â  â     âââ ...
âÂ Â  âââ catalogue
â   âÂ Â  âââ urls.py
â   âÂ Â  âââ views.py
âÂ Â  â     âââ ...
âÂ Â  âââ checkout
â   âÂ Â  âââ urls.py
â   âÂ Â  âââ views.py
âÂ Â  â     âââ ...
âÂ Â  âââ core
â   âÂ Â  âââ urls.py
â   âÂ Â  âââ views.py
âÂ Â  â     âââ ...
âÂ Â  âââ customer
âÂ Â  âââ dashboard
âÂ Â  âââ  __init__.py
âââ __init__.py
âââ manage.py
âââ project_static
âÂ Â  âââ assets
âÂ Â  âââ bower_components
âÂ Â  âââ js
âââ public
âÂ Â  âââ emails
âÂ Â  âââ media
âÂ Â  âââ static
âââ settings
âÂ Â  âââ base.py
âÂ Â  âââ dev.py
âÂ Â  âââ __init__.py
âÂ Â  âââ local.py
âÂ Â  âââ production.py
âââ templates
âÂ Â  âââ base.html
âÂ Â  âââ basket
âÂ Â  âââ blog
âÂ Â  âââ ....
âââ urls.py
âââ venv
âÂ Â  âââ bin
âÂ Â  âââ include
âÂ Â  âââ lib
âÂ Â  âââ pip-selfcheck.json
âÂ Â  âââ share
âââ wsgi.py
</code></pre>

<p>So, there's no 'main' module running the show, which is what I expect gunicorn is looking for.</p>

<p>any thoughts?</p>

<p><strong>wsgi.py</strong>:</p>

<pre><code>import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

application = get_wsgi_application()
</code></pre>

<p>Your error message is </p>

<pre><code>ImportError: No module named 'myproject.wsgi'
</code></pre>

<p>You ran the app with</p>

<pre><code>gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
</code></pre>

<p>And wsgi.py has the line</p>

<pre><code>os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
</code></pre>

<p>This is the disconnect.  In order to recognize the project as <code>myproject.wsgi</code> the <em>parent</em> directory would have to be on the python path... running </p>

<pre><code>cd .. &amp;&amp; gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
</code></pre>

<p>Would eliminate that error.  However, you would then get a different error because the wsgi.py file refers to <code>settings</code> instead of <code>myproject.settings</code>.  This implies that the app was intended to be run from the root directory instead of one directory up.  You can figure this out for sure by looking at the code- if it uses absolute imports, do they usually say <code>from myproject.app import ...</code> or <code>from app import ...</code>.  If that guess is correct, your correct commmand is</p>

<pre><code>gunicorn --bind 0.0.0.0:8000 wsgi:application
</code></pre>

<p>If the app does use <code>myproject</code> in all of the paths, you'll have to modify your PYTHONPATH to run it properly...</p>

<pre><code>PYTHONPATH=`pwd`/.. gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
</code></pre>

python django nginx gunicorn