gunicorn由于__init__.py中的导入而失败

时间:2014-05-02 20:21:35

标签: python django gunicorn

我在我的应用 init .py:

中有这一行
from .signals import *

当我运行gunicorn myproject.wsgi:application时,它会给我这个错误:

Traceback (most recent call last):
  File "/webapps/venv/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
    worker.init_process()
  File "/webapps/venv/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
    self.wsgi = self.app.wsgi()
  File "/webapps/venv/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
    self.callable = self.load()
  File "/webapps/venv/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
    return self.load_wsgiapp()
  File "/webapps/venv/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/webapps/venv/local/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
    __import__(module)
  File "/webapps/venv/myproject/myproject/__init__.py", line 1, in <module>
    from .signals import *
  File "/webapps/venv/myproject/myproject/signals.py", line 1, in <module>
    from django.db.models.signals import pre_delete
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/__init__.py", line 5, in <module>
    from django.db.models.query import Q
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 17, in <module>
    from django.db.models.deletion import Collector
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/deletion.py", line 4, in <module>
    from django.db.models import signals, sql
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/sql/__init__.py", line 4, in <module>
    from django.db.models.sql.subqueries import *
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/sql/subqueries.py", line 12, in <module>
    from django.db.models.sql.query import Query
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 22, in <module>
    from django.db.models.sql import aggregates as base_aggregates_module
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/sql/aggregates.py", line 9, in <module>
    ordinal_aggregate_field = IntegerField()
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 116, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "/webapps/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
    self._setup(name)
  File "/webapps/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 47, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Traceback (most recent call last):
  File "/webapps/venv/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
    worker.init_process()
  File "/webapps/venv/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
    self.wsgi = self.app.wsgi()
  File "/webapps/venv/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
    self.callable = self.load()
  File "/webapps/venv/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
    return self.load_wsgiapp()
  File "/webapps/venv/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/webapps/venv/local/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
    __import__(module)
  File "/webapps/venv/myproject/myproject/__init__.py", line 1, in <module>
    from .signals import *
  File "/webapps/venv/myproject/myproject/signals.py", line 1, in <module>
    from django.db.models.signals import pre_delete
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/__init__.py", line 5, in <module>
    from django.db.models.query import Q
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 17, in <module>
    from django.db.models.deletion import Collector
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/deletion.py", line 4, in <module>
    from django.db.models import signals, sql
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/sql/__init__.py", line 4, in <module>
    from django.db.models.sql.subqueries import *
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/sql/subqueries.py", line 12, in <module>
    from django.db.models.sql.query import Query
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 22, in <module>
    from django.db.models.sql import aggregates as base_aggregates_module
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/sql/aggregates.py", line 9, in <module>
    ordinal_aggregate_field = IntegerField()
  File "/webapps/venv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 116, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "/webapps/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
    self._setup(name)
  File "/webapps/venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 47, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

如果我在__init__.py中禁用上述行,则gunicorn将开始工作。 当然,我可以将import行移到另一个地方并解决问题,但我想知道为什么会发生错误,以及是否有另一种解决方法。

1 个答案:

答案 0 :(得分:0)

通过使用from .signals import *,您似乎因执行顺序而收到错误。

根据DJANGO_SETTINGS_MODULE的错误,您需要定义以便从模型文件导入。我认为正在发生的问题是当gunicorn加载你的应用程序时,但在Django实际加载了所有应用程序设置之前,__init__.py文件正在执行。

可能的修复方法可能是定义__all__文件中的signals.py

虽然没有更多的问题背景,但很难知道。