Django не может импортировать WSGI после реструктуризации?

python django wsgi gunicorn django-wsgi

91 просмотра

1 ответ

Моя первоначальная структура проекта выглядит следующим образом:

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:

Я должен добавить src.ко всем foo.:

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

Есть ли лучшие способы сделать это?

Автор: laukok Источник Размещён: 19.09.2019 11:23

Ответы (1)


0 плюса

manage.py здесь совершенно не имеет значения, так как вы не используете его, когда запускаете gunicorn.

Проблема заключается в том, вероятно , что вам нужно пустой __init__.pyвнутри fooкаталога , чтобы сделать это пакет , который Python может импортировать.

Автор: Daniel Roseman Размещён: 20.05.2017 04:15
Вопросы из категории :
32x32