Deploying Django with gunicorn No module named Imp

2019-05-11 00:03发布

问题:

I m trying to deploy my Django project with using nginx, gunicorn and virtualenv. But i m getting following error while run this comment

sudo gunicorn_django --bind test.com:8001

Log :

Traceback (most recent call last):
  File "/opt/postjust/lib/python2.7/site-packages/gunicorn/arbiter.py", line 503, in spawn_worker
    worker.init_process()
  File "/opt/postjust/lib/python2.7/site-packages/gunicorn/workers/base.py", line 116, in init_process
    self.wsgi = self.app.wsgi()
  File "/opt/postjust/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/opt/postjust/lib/python2.7/site-packages/gunicorn/app/djangoapp.py", line 105, in load
    mod = util.import_module("gunicorn.app.django_wsgi")
  File "/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/opt/postjust/lib/python2.7/site-packages/gunicorn/app/django_wsgi.py", line 21, in <module>
    from django.core.management.validation import get_validation_errors
ImportError: No module named validation
[2015-02-23 00:58:17 +0200] [10584] [INFO] Worker exiting (pid: 10584)
[2015-02-23 00:58:17 +0200] [10581] [INFO] Shutting down: Master
[2015-02-23 00:58:17 +0200] [10581] [INFO] Reason: Worker failed to boot.

The project runs fine on local.

this is my settings.py file :

"""
Django settings for postjust project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'jb-@(98(ew4kkociwv+2y(3799r*vug7-$g)e=6wsxigrk30=!'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

TEMPLATE_DEBUG = False

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'postjust.urls'

WSGI_APPLICATION = 'postjust.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

my pip list on virtualenv :

Django (1.7.4)

gunicorn (19.2.1)

pip (6.0.8)

setuptools (12.0.5)

--Python 2.7.8

回答1:

Changing gunicorn_django to gunicorn newApp.wsgi:application should fix this.

Using gunicorn_django is no longer recommended. This is because it calls django_wsgi.py which is deprecated and throws the import error.

More info: http://www.rkblog.rk.edu.pl/w/p/first-impressions-django-17-beta-upgrade-young-project/



回答2:

It looks like django wsgi file for gunicorn has django dependency. (Last line of stack trace.) https://github.com/benoitc/gunicorn/blob/e0b3c42dd2c31b2f60abd6833401bd8eed116dc6/gunicorn/app/django_wsgi.py#L21

It looks to me like gunicorn cannot find django on its path.