我的初始项目结构是这样的:
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'
有更好的方法吗?
答案 0 :(得分:0)
manage.py
在这里完全不相关,因为你在开枪时不使用它。
问题可能是你需要__init__.py
目录中的空foo
来使它成为Python可以导入的包。