【问题标题】:django fails to import wsgi after restructuring?django 重组后无法导入wsgi?
【发布时间】:2017-05-21 00:43:24
【问题描述】:

我最初的项目结构是这样的:

manage.py
foo/
  wsgi.py
  settings.py
  urls.py
  __init__.py

manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "foo.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

新结构:

manage.py
src/
   foo/
      wsgi.py
      settings.py
      urls.py
      __init__.py

manage.py

#!/usr/bin/env python
import os
import sys
project_root = os.path.dirname(os.path.abspath(__file__))

print project_root 

if __name__ == "__main__":
    sys.path.insert(0, os.path.join(project_root, 'src'))

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

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

然后在部署过程中,出现ImportError: No module named foo.wsgi这个错误:

$ gunicorn foo.wsgi:application 
2017-05-20 16:53:31 [19107] [INFO] Starting gunicorn 18.0
2017-05-20 16:53:31 [19107] [INFO] Listening at: http://127.0.0.1:8000 (19107)
2017-05-20 16:53:31 [19107] [INFO] Using worker: sync
2017-05-20 16:53:31 [19111] [INFO] Booting worker with pid: 19111
2017-05-20 16:53:31 [19111] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
    worker.init_process()
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
    self.wsgi = self.app.wsgi()
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
    self.callable = self.load()
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
    return self.load_wsgiapp()
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
    __import__(module)
ImportError: No module named foo.wsgi
Traceback (most recent call last):
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
    worker.init_process()
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
    self.wsgi = self.app.wsgi()
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
    self.callable = self.load()
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
    return self.load_wsgiapp()
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
    __import__(module)
ImportError: No module named foo.wsgi
2017-05-20 16:53:31 [19111] [INFO] Worker exiting (pid: 19111)
2017-05-20 16:53:31 [19107] [INFO] Shutting down: Master
2017-05-20 16:53:31 [19107] [INFO] Reason: Worker failed to boot.

django重组后导入wsgi失败

wsgi.py

import os

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

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

有什么想法吗?

编辑:

我让它运行:

$ gunicorn src.foo.wsgi:application 
2017-05-20 17:33:52 [22227] [INFO] Starting gunicorn 18.0
2017-05-20 17:33:52 [22227] [INFO] Listening at: http://127.0.0.1:8000 (22227)
2017-05-20 17:33:52 [22227] [INFO] Using worker: sync
2017-05-20 17:33:52 [22231] [INFO] Booting worker with pid: 22231

但是当我访问http://127.0.0.1:8000/ 的站点时,我收到了这个错误:

2017-05-20 17:33:59 [22231] [ERROR] Error handling request
Traceback (most recent call last):
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 131, in handle_request$ gunicorn src.foo.wsgi:application 
2017-05-20 17:33:52 [22227] [INFO] Starting gunicorn 18.0
2017-05-20 17:33:52 [22227] [INFO] Listening at: http://127.0.0.1:8000 (22227)
2017-05-20 17:33:52 [22227] [INFO] Using worker: sync
2017-05-20 17:33:52 [22231] [INFO] Booting worker with pid: 22231
2017-05-20 17:33:59 [22231] [ERROR] Error handling request
Traceback (most recent call last):
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 131, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 43, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/www/html/django-project/foo/env/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 'foo.settings' (Is it on sys.path?): No module named foo.settings
2017-05-20 17:33:59 [22231] [ERROR] Error handling request
Traceback (most recent call last):
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 131, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 43, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/www/html/django-project/foo/env/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 'foo.settings' (Is it on sys.path?): No module named foo.settings

    respiter = self.wsgi(environ, resp.start_response)
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 43, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/www/html/django-project/foo/env/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 'foo.settings' (Is it on sys.path?): No module named foo.settings
2017-05-20 17:33:59 [22231] [ERROR] Error handling request
Traceback (most recent call last):
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 131, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 43, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/var/www/html/django-project/foo/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/www/html/django-project/foo/env/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 'foo.settings' (Is it on sys.path?): No module named foo.settings

编辑 2:

我必须给所有foo.添加一个src.

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.foo.settings")
ROOT_URLCONF = 'src.foo.urls'

还有更好的方法吗?

【问题讨论】:

  • manage.py 是用来在 gunicorn 中启动服务器的(没用过)。如果你可以manage.py runserver ok,那么我猜gunicorn 还没有得到src 到它的PYTHONPATH..
  • @thebjorn Is manage.py used to start the server in gunicorn (never used it) 你是什么意思?
  • 如何让 gunicorn 启动服务器?您正在manage.py 中修复您的路径,也许您在从 gunicorn 启动服务器时需要做类似的事情?
  • @thebjorn 这是如何启动它(env) xxx@xxx-desktop:/var/www/html/django-project/foo/foo$ gunicorn foo.wsgi:application
  • 请阅读回溯。无论如何,这些都是新问题(我必须带狗出去玩一下……祝你好运;-)

标签: python django wsgi gunicorn django-wsgi


【解决方案1】:

manage.py 在这里完全无关紧要,因为您在启动 gunicorn 时不会使用它。

问题可能是您需要在foo 目录中创建一个空的__init__.py 以使其成为Python 可以导入的包。

【讨论】:

  • 现在可以运行了,但有点乏味。看我的编辑 2。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
  • 2020-06-04
  • 2017-07-26
  • 2012-11-25
  • 2011-07-04
  • 1970-01-01
相关资源
最近更新 更多