I'm installing a previously built website on a new server. I'm not the original developer.
I've used Gunicorn + nginx in the past to keep the app alive (basically following this tutorial), but am having problems with it here.
I
source venv/bin/activate
, then./manage.py runserver 0.0.0.0:8000
works well and everything is running as expected. I shut it down and rungunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
, and get the following:[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.
I believe it has something to do with the structure of the whole application. Before, I've built apps with the basic structure of:
myproject βββ manage.py βββ myproject βΒ Β βββ urls.py βΒ Β βββ views.py βΒ Β βββ component1 β βΒ Β βββ urls.py β βΒ Β βββ views.py βΒ Β βββ component2 β βΒ Β βββ urls.py β βΒ Β βββ views.py βββ venv βΒ Β βββ bin βΒ Β βββ ...
This one, instead, has a structure like:
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
So, there's no 'main' module running the show, which is what I expect gunicorn is looking for.
Any thoughts?
wsgi.py:
import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") application = get_wsgi_application()
score:113
Your error message is
ImportError: No module named 'myproject.wsgi'
You ran the app with
gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
And wsgi.py has the line
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
This is the disconnect. In order to recognize the project as myproject.wsgi
the parent directory would have to be on the python path... running
cd .. && gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
Would eliminate that error. However, you would then get a different error because the wsgi.py file refers to settings
instead of myproject.settings
. 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 from myproject.app import ...
or from app import ...
. If that guess is correct, your correct commmand is
gunicorn --bind 0.0.0.0:8000 wsgi:application
If the app does use myproject
in all of the paths, you'll have to modify your PYTHONPATH to run it properly...
PYTHONPATH=`pwd`/.. gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
Similar question
- django + virtualenv + gunicorn - No module named django.core.wsgi?
- Django Gunicorn ImportError: No module named django.core.wsgi
- Deploying Django with gunicorn No module named ImportError: No module named validation
- No module named django.core when running django-admin startproject myproject
- ImportError: No module named 'environ' with gunicorn on django
- Django celery ImportError: no module named celery when using gunicorn bind?
- setting up gunicorn and django: No module named revamp.wsgi
- Django - gunicorn on App Engine. Error: 'No Module Named my_project'
- Gunicorn ImportError: No module named
- ModuleNotFoundError: No module named 'App Name' when using Django and Gunicorn on a heroku server
score:0
I faced a similar problem. The gunicorn was being run by a globally installed package, not the one that was installed in the virtual environment. This answer helped me to figure it out.
score:0
I was facing similar issue. I recreated the virtual environment and install gunicorn using pip3 (not using apt) and it worked fine
score:1
If you are using supervisor
then you have to set environment
in supervisor config file as bellow.
environment=HOME="/home/to/your/project/root"
score:2
Run these command after replacing your python working directory path.
# Go to your current working directory
cd /path/to/folder
# Activate your virtual environment. Ignore if already in activated mode
source /path/to/virtualenv/bin/activate
# Install gunicorn in virtualenv
pip3 install gunicorn
# Run this command. Replace PORT and app name accordingly
gunicorn --bind 0.0.0.0:5000 wsgi:app
score:5
For my side, My project structure is
myproject
βββ manage.py
βββ myproject
β βββ wsgi.py
β βββ ..
Dockerfile
docker-composer.yml
So in docker-composer.yml, when command
gunicorn myproject.wsgi:application --bind 0.0.0.0:8000
i get following error
ModuleNotFoundError: No module named 'myproject.wsgi'
What we have to do is, we must run gunicorn command inside folder, not project root. This is the working code
sh -c "cd ./myproject && gunicorn myproject.wsgi:application --bind 0.0.0.0:8000"
Before gunicorn command, we have to change directory with "cd ./project". Inside the "myproject" directory, gunicorn can recognise our projects clearly.
Credit To: stackoverflow.com
Related Query
- gunicorn ImportError: No module named django_jalali
- No module named pkg_resources
- No module named MySQLdb
- ImportError: No module named 'django.core.urlresolvers'
- django import error - No module named core.management
- Django Rest Framework -- no module named rest_framework
- Django - "no module named django.core.management"
- Error: No module named psycopg2.extensions
- django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
- Django import error - no module named django.conf.urls.defaults
- Gunicorn, no module named 'myproject
- ImportError: No module named virtualenv
- django.core.exceptions.ImproperlyConfigured: Error loading psycopg module: No module named psycopg
- "No module named simple" error in Django
- Running django tutorial tests fail - No module named polls.tests
- ImportError: No module named django.core.wsgi Apache + VirtualEnv + AWS + WSGI
- ImportError: No module named django_filters
- No module named 'polls.apps.PollsConfigdjango'; Django project tutorial 2
- No module named django but it is installed
- ImportError: No module named django.core.handlers.wsgi in install django mod_wsgi config on apache
More Query from same tag
- How do I do this "order by" in Django, if I have foreign keys?
- How to add a value to the queryset?
- I don't need django tables by default
- How do I receive subdomain wildcards on Apache2 using mod wsgi?
- Django: Number of ForeignKey Database Queries
- Passing params from Angularjs to Django
- Sporadic corruption of files being uploaded
- SSL Using Gunicorn for Django
- Django - How to prepopulate admin form fields
- Django app runs locally but I get CSRF verification failed on Heroku
- Creating a detail view with get_context_data grabbing data from url
- I am trying to perform a filter, count, and sort on a Django 1.6 dataset
- How should I write view unittest in Django?
- Django: Password protect photo url's?
- Can somebody explain me what is the use of "etag" in django?