gunicorn django导入错误'无法导入设置''无法导入名称StaticFilesHandler'

时间:2014-02-20 08:56:45

标签: django settings gunicorn pythonpath

运行shahar@shahar-desktop:~/proj/nopain/src/django⟫ gunicorn nopain.wsgi会在工作进程中出现错误:一个ImportError:

正常运行python manage.py runserver

我正在尝试调试,但似乎python路径没问题......请帮忙。

2014-02-20 10:33:22 [6851] [INFO] Starting gunicorn 18.0
2014-02-20 10:33:22 [6851] [INFO] Listening at: http://127.0.0.1:8000 (6851)
2014-02-20 10:33:22 [6851] [INFO] Using worker: sync
2014-02-20 10:33:22 [6856] [INFO] Booting worker with pid: 6856
2014-02-20 02:33:23 [6856] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
    worker.init_process()
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
    self.wsgi = self.app.wsgi()
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
    self.callable = self.load()
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
    return self.load_wsgiapp()
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
    __import__(module)
  File "/home/shahar/proj/nopain/src/django/nopain/wsgi.py", line 29, in <module>
    from dj_static import Cling
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/dj_static.py", line 8, in <module>
    from django.contrib.staticfiles.handlers import StaticFilesHandler as DebugHandler
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 13, in <module>
    from django.contrib.staticfiles.views import serve
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/contrib/staticfiles/views.py", line 16, in <module>
    from django.views import static
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/views/static.py", line 98, in <module>
    template_translatable = ugettext_noop("Index of %(directory)s")
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 60, in gettext_noop
    return _trans.gettext_noop(message)
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 47, in __getattr__
    if settings.USE_I18N:
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'nopain.settings' (Is it on sys.path?): cannot import name StaticFilesHandler
Traceback (most recent call last):
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
    worker.init_process()
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
    self.wsgi = self.app.wsgi()
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
    self.callable = self.load()
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
    return self.load_wsgiapp()
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
    __import__(module)
  File "/home/shahar/proj/nopain/src/django/nopain/wsgi.py", line 29, in <module>
    from dj_static import Cling
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/dj_static.py", line 8, in <module>
    from django.contrib.staticfiles.handlers import StaticFilesHandler as DebugHandler
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 13, in <module>
    from django.contrib.staticfiles.views import serve
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/contrib/staticfiles/views.py", line 16, in <module>
    from django.views import static
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/views/static.py", line 98, in <module>
    template_translatable = ugettext_noop("Index of %(directory)s")
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 60, in gettext_noop
    return _trans.gettext_noop(message)
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 47, in __getattr__
    if settings.USE_I18N:
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/shahar/.virtualenvs/nopain/local/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'nopain.settings' (Is it on sys.path?): cannot import name StaticFilesHandler
2014-02-20 02:33:23 [6856] [INFO] Worker exiting (pid: 6856)
2014-02-20 10:33:23 [6851] [INFO] Shutting down: Master
2014-02-20 10:33:23 [6851] [INFO] Reason: Worker failed to boot.

似乎我需要添加更多细节......根据stackoverflow中的红色弹出窗口。所以这里有一些毫无意义的文字。

这是我的nopain/wsgi.py

import os
import sys

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

from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())

这是我的nopain/settings.py

# -*- coding: utf-8 -*-
"""Settings file for `nopain.co.il`."""

###############################################################################
# BASE AND HELPER STATEMENTS ##################################################

import os
from django.utils.translation import ugettext

SITE_ID = 1

PROJECT_PATH = os.path.split(os.path.abspath(os.path.dirname(__file__)))[0]

MIDDLEWARE_CLASSES = (
    'django.middleware.locale.LocaleMiddleware',
    'sslifyadmin.middleware.SSLifyAdminMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'axes.middleware.FailedLoginMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'cms.middleware.page.CurrentPageMiddleware',
    'cms.middleware.user.CurrentUserMiddleware',
    'cms.middleware.toolbar.ToolbarMiddleware',
    'cms.middleware.language.LanguageCookieMiddleware',
)

ROOT_URLCONF = 'nopain.urls'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'south',
    'djangojs',
)

SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'


###############################################################################
# DEBUGGING ###################################################################

if os.environ.get('DEBUG') == 'True':
    DEBUG = True
else:
    DEBUG = False

TEMPLATE_DEBUG = DEBUG


###############################################################################
# EMAILS ######################################################################

ADMINS = (
    ('Shahar Or', 'mightyiampresence@gmail.com'),
)

MANAGERS = ADMINS

SEND_BROKEN_LINK_EMAILS = True


###############################################################################
# DATABASE ####################################################################

INSTALLED_APPS += ('dj_database_url',)

import dj_database_url
DATABASES = {'default': dj_database_url.config()}


###############################################################################
# SECURITY ####################################################################

ALLOWED_HOSTS = ['*']

SECRET_KEY = os.environ.get('SECRET_KEY')

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

INSTALLED_APPS += ('axes',)

AXES_LOGIN_FAILURE_LIMIT = 5
AXES_LOCK_OUT_AT_FAILURE = True
AXES_USE_USER_AGENT = False
AXES_COOLOFF_TIME = 1
AXES_LOGGER = 'axes.watch_login'
AXES_LOCKOUT_TEMPLATE = None
AXES_LOCKOUT_URL = None
AXES_VERBOSE = True

###############################################################################
# STATIC FILES ################################################################

INSTALLED_APPS += ('django.contrib.staticfiles',)

STATIC_ROOT = os.path.join(PROJECT_PATH, 'staticfiles')

STATIC_URL = '/static/'

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

STATICFILES_DIRS = (os.path.join(PROJECT_PATH, 'static'),)


###############################################################################
# TEMPLATES ###################################################################

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

TEMPLATE_DIRS = (os.path.join(PROJECT_PATH, 'templates'),)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.i18n',
    'django.core.context_processors.request',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'cms.context_processors.media',
    'sekizai.context_processors.sekizai',
)

CMS_TEMPLATES = (
    ('base.html', ugettext('Base template')),
    ('home.html', ugettext('Home page template')),
    ('text-page.html', ugettext('Text page template')),
    ('contact.html', ugettext('Contact page template')),
    ('article.html', ugettext('Article')),
    ('videos.html', ugettext('Videos')),
    ('gallery.html', ugettext('Gallery')),
)


###############################################################################
# I18N ########################################################################

USE_I18N = True
LANGUAGE_CODE = 'en-us'
LANGUAGES = [
    ('en', u'English\u200B'),
    ('he', u'עברית'),
    ('de', u'Deutsche'),
]

LOCALE_PATHS = (os.path.join(PROJECT_PATH, 'conf/locale'),)

USE_TZ = True
TIME_ZONE = 'Asia/Jerusalem'

USE_L10N = True

INSTALLED_APPS += (
    'bidiutils',
)

TEMPLATE_CONTEXT_PROCESSORS += (
    'bidiutils.context_processors.bidi',
)


###############################################################################
# WSGI ########################################################################

WSGI_APPLICATION = 'nopain.wsgi.application'


###############################################################################
# MEDIA #######################################################################

if os.environ.get('MEDIA_STORAGE') == 'local':
    MEDIA_ROOT = os.path.join(
        PROJECT_PATH,
        'media/'
    )
    MEDIA_URL = '/media/'
    THUMBNAIL_DEFAULT_STORAGE = \
        'easy_thumbnails.storage.ThumbnailFileSystemStorage'

elif os.environ.get('MEDIA_STORAGE') == 'remote':
    INSTALLED_APPS += ('storages',)
    DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
    THUMBNAIL_DEFAULT_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
    AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
    AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
    AWS_STORAGE_BUCKET_NAME = 'nopain-media-files'
    MEDIA_URL = 'https://s3-eu-west-1.amazonaws.com/%s/media/'\
        % os.environ.get('AWS_S3_MEDIA_BUCKET_NAME')
    MEDIA_ROOT = 'media/'

INSTALLED_APPS += (
    'easy_thumbnails',
)

THUMBNAIL_BASEDIR = 'thumbs'

THUMBNAIL_DEBUG = DEBUG

THUMBNAIL_MEDIA_ROOT = MEDIA_ROOT
THUMBNAIL_MEDIA_URL = MEDIA_URL


###############################################################################
# CMS #########################################################################

INSTALLED_APPS += (
    'cms',
    'mptt',
    'menus',
    'sekizai',
    'cms.plugins.text',
    'reversion',
    'cmsplugin_gallery',
    'cms.plugins.picture',
    'cms.plugins.link',
    'cms.plugins.googlemap',
    'cmsplugin_video_youtube',
)

# PLACEHOLDERS

CMS_PLACEHOLDER_CONF = {
    'text': {
        'plugins': ['TextPlugin'],
        'name': ugettext('Text'),
        'limits': {
            'global': 1,
            'TextPlugin': 1,
        },
    },
    'text2': {
        'plugins': ['TextPlugin'],
        'name': ugettext('Text'),
        'limits': {
            'global': 1,
            'TextPlugin': 1,
        },
    },
    'gallery': {
        'name': ugettext('Gallery'),
        'plugins': ['CMSGalleryPlugin'],
        'limits': {
            'global': 1,
            'CMSGalleryPlugin': 1,
        },
    },
    'google_map': {
        'name': ugettext('Google Map'),
        'plugins': ['GoogleMapPlugin'],
        'limits': {
            'global': 1,
            'GoogleMapPlugin': 1,
        },
    },
    'youtubes_preface': {
        'name': ugettext('Preface'),
        'plugins': ['TextPlugin'],
        'limits': {
            'global': 1,
            'TextPlugin': 1,
        },
    },
    'youtubes_header_testimonials': {
        'name': ugettext('Testimonials header'),
        'plugins': ['TextPlugin'],
        'limits': {
            'global': 1,
            'TextPlugin': 1,
        },
    },
    'youtubes_testimonials': {
        'name': ugettext('Testimonial YouTubes'),
        'plugins': ['YouTubeVideoPlugin'],
    },
    'youtubes_header_paula': {
        'name': ugettext('Paula header'),
        'plugins': ['TextPlugin'],
        'limits': {
            'global': 1,
            'TextPlugin': 1,
        },
    },
    'youtubes_paula': {
        'name': ugettext('Paula YouTubes'),
        'plugins': ['YouTubeVideoPlugin'],
    },
    'youtubes_header_mds': {
        'name': ugettext('MD\'s header'),
        'plugins': ['TextPlugin'],
        'limits': {
            'global': 1,
            'TextPlugin': 1,
        },
    },
    'youtubes_mds': {
        'name': ugettext('MD\'s YouTubes'),
        'plugins': ['YouTubeVideoPlugin'],
    },
    'youtubes_copyright': {
        'name': ugettext('Copyright notice'),
        'plugins': ['TextPlugin'],
        'limits': {
            'global': 1,
            'TextPlugin': 1,
        },
    },
}

###############################################################################
# WYMEDITOR ###################################################################

WYM_TOOLS = ",\n".join([
    "{'name': 'Bold', 'title': 'Strong', 'css': 'wym_tools_strong'}",
    "{'name': 'Italic', 'title': 'Emphasis', 'css': 'wym_tools_emphasis'}",
    "{'name': 'Superscript', 'title': 'Superscript',\
    'css': 'wym_tools_superscript'}",
    "{'name': 'Subscript', 'title': 'Subscript',\
    'css': 'wym_tools_subscript'}",
    "{'name': 'InsertOrderedList', 'title': 'Ordered_List',\
    'css': 'wym_tools_ordered_list'}",
    "{'name': 'InsertUnorderedList', 'title': 'Unordered_List',\
    'css': 'wym_tools_unordered_list'}",
    "{'name': 'Indent', 'title': 'Indent', 'css': 'wym_tools_indent'}",
    "{'name': 'Outdent', 'title': 'Outdent', 'css': 'wym_tools_outdent'}",
    "{'name': 'Undo', 'title': 'Undo', 'css': 'wym_tools_undo'}",
    "{'name': 'Redo', 'title': 'Redo', 'css': 'wym_tools_redo'}",
    "{'name': 'Paste', 'title': 'Paste_From_Word', 'css': 'wym_tools_paste'}",
    "{'name': 'ToggleHtml', 'title': 'HTML', 'css': 'wym_tools_html'}",
    "{'name': 'CreateLink', 'title': 'Link', 'css': 'wym_tools_link'}",
    "{'name': 'Unlink', 'title': 'Unlink', 'css': 'wym_tools_unlink'}",
    #"{'name': 'InsertImage', 'title': 'Image', 'css': 'wym_tools_image'}",
    "{'name': 'InsertTable', 'title': 'Table', 'css': 'wym_tools_table'}",
    #"{'name': 'Preview', 'title': 'Preview', 'css': 'wym_tools_preview'}",
    "{'name': 'DirectionLtr', 'title': 'Left_to_Right',\
    'css': 'wym_tools_direction_ltr'}",
    "{'name': 'DirectionRtl', 'title': 'Right_to_Left',\
    'css': 'wym_tools_direction_rtl'}",
    "{'name': 'DirectionRemove', 'title': 'Remove_Direction',\
    'css': 'wym_tools_direction_remove'}",
])

1 个答案:

答案 0 :(得分:1)

所以我通过以下方式解决了这个问题:

  1. 不是gunicorn nopain.wsgi而是python manage.py run_gunicorn {需要gunicorn中的INSTALLED_APPS
  2. 我发现这样我必须settings.LOGGINGversionsettings.LOGGING_CONFIG = None(或者是False?)