Gunicorn + Django: ImportError: no module named
Gunicorn + Django: ImportError: no module named
我正在尝试使用 nginx 和 Gunicorn 设置现有的 Django 应用程序。在完成 nginx 设置和测试之前,我只是尝试使用 gunicorn
进行测试。当我尝试 运行 gunicorn
命令时出现以下错误:
(venv)me@mymachine:/srv/test/proj$ gunicorn --bind 0.0.0.0:8001 myapp.wsgi
[2015-09-18 09:40:30 -0400] [18838] [INFO] Starting gunicorn 19.3.0
[2015-09-18 09:40:30 -0400] [18838] [INFO] Listening at: http://0.0.0.0:8001 (18838)
[2015-09-18 09:40:30 -0400] [18838] [INFO] Using worker: sync
[2015-09-18 09:40:30 -0400] [18841] [INFO] Booting worker with pid: 18841
/srv/test/venv/lib/python3.4/importlib/_bootstrap.py:321: RemovedInDjango19Warning: The django.forms.util module has been renamed. Use django.forms.utils instead.
return f(*args, **kwds)
[2015-09-18 09:40:31 -0400] [18841] [ERROR] Exception in worker process:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/srv/test/venv/lib/python3.4/site-packages/gunicorn/arbiter.py", line 507, in spawn_worker
worker.init_process()
File "/srv/test/venv/lib/python3.4/site-packages/gunicorn/workers/base.py", line 118, in init_process
self.wsgi = self.app.wsgi()
File "/srv/test/venv/lib/python3.4/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/srv/test/venv/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
return self.load_wsgiapp()
File "/srv/test/venv/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
return util.import_app(self.app_uri)
File "/srv/test/venv/lib/python3.4/site-packages/gunicorn/util.py", line 355, in import_app
__import__(module)
File "/srv/test/proj/myapp/wsgi.py", line 30, in <module>
application = get_wsgi_application()
File "/srv/test/venv/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/srv/test/venv/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/srv/test/venv/lib/python3.4/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/srv/test/venv/lib/python3.4/site-packages/django/contrib/admin/apps.py", line 22, in ready
self.module.autodiscover()
File "/srv/test/venv/lib/python3.4/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/srv/test/venv/lib/python3.4/site-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/srv/test/venv/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/srv/test/proj/request/admin.py", line 6, in <module>
from resource.models import ResourceRequest
ImportError: No module named 'resource.models'; 'resource' is not a package
但是该文件确实存在并且被设置为一个包。我什至可以做到 runserver
并且开发服务器正确启动并出现 0 个错误。
这是我的 wsgi.py:
"""
WSGI config for myapp project.
"""
import os
from django.core.wsgi import get_wsgi_application
# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "myapp.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
application = get_wsgi_application()
我进行了高低搜索,Whosebug 或其他网站上的 none 答案完全有帮助。我在 python2 和 python3 环境中都试过了。
我也试过:
gunicorn --bind 0.0.0.0:8001 myapp.wsgi:application
和
gunicorn --env DJANGO_SETTINGS_MODULE=myapp.settings myapp.wsgi:application
两者都有相同的错误。
可能是什么问题?
编辑:我认为这是错误的关键部分:
[2015-09-18 10:30:32 -0400] [19235] [ERROR] Exception in worker process:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
这是 Gunicorn 中的错误吗?
想通了。 resource
一词显然是 Gunicorn 的某种名称冲突。必须重命名我的应用程序和所有对 resources
的引用才能修复它。
等等...
我正在尝试使用 nginx 和 Gunicorn 设置现有的 Django 应用程序。在完成 nginx 设置和测试之前,我只是尝试使用 gunicorn
进行测试。当我尝试 运行 gunicorn
命令时出现以下错误:
(venv)me@mymachine:/srv/test/proj$ gunicorn --bind 0.0.0.0:8001 myapp.wsgi
[2015-09-18 09:40:30 -0400] [18838] [INFO] Starting gunicorn 19.3.0
[2015-09-18 09:40:30 -0400] [18838] [INFO] Listening at: http://0.0.0.0:8001 (18838)
[2015-09-18 09:40:30 -0400] [18838] [INFO] Using worker: sync
[2015-09-18 09:40:30 -0400] [18841] [INFO] Booting worker with pid: 18841
/srv/test/venv/lib/python3.4/importlib/_bootstrap.py:321: RemovedInDjango19Warning: The django.forms.util module has been renamed. Use django.forms.utils instead.
return f(*args, **kwds)
[2015-09-18 09:40:31 -0400] [18841] [ERROR] Exception in worker process:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/srv/test/venv/lib/python3.4/site-packages/gunicorn/arbiter.py", line 507, in spawn_worker
worker.init_process()
File "/srv/test/venv/lib/python3.4/site-packages/gunicorn/workers/base.py", line 118, in init_process
self.wsgi = self.app.wsgi()
File "/srv/test/venv/lib/python3.4/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/srv/test/venv/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
return self.load_wsgiapp()
File "/srv/test/venv/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
return util.import_app(self.app_uri)
File "/srv/test/venv/lib/python3.4/site-packages/gunicorn/util.py", line 355, in import_app
__import__(module)
File "/srv/test/proj/myapp/wsgi.py", line 30, in <module>
application = get_wsgi_application()
File "/srv/test/venv/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/srv/test/venv/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/srv/test/venv/lib/python3.4/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/srv/test/venv/lib/python3.4/site-packages/django/contrib/admin/apps.py", line 22, in ready
self.module.autodiscover()
File "/srv/test/venv/lib/python3.4/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/srv/test/venv/lib/python3.4/site-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/srv/test/venv/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/srv/test/proj/request/admin.py", line 6, in <module>
from resource.models import ResourceRequest
ImportError: No module named 'resource.models'; 'resource' is not a package
但是该文件确实存在并且被设置为一个包。我什至可以做到 runserver
并且开发服务器正确启动并出现 0 个错误。
这是我的 wsgi.py:
"""
WSGI config for myapp project.
"""
import os
from django.core.wsgi import get_wsgi_application
# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "myapp.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
application = get_wsgi_application()
我进行了高低搜索,Whosebug 或其他网站上的 none 答案完全有帮助。我在 python2 和 python3 环境中都试过了。
我也试过:
gunicorn --bind 0.0.0.0:8001 myapp.wsgi:application
和
gunicorn --env DJANGO_SETTINGS_MODULE=myapp.settings myapp.wsgi:application
两者都有相同的错误。
可能是什么问题?
编辑:我认为这是错误的关键部分:
[2015-09-18 10:30:32 -0400] [19235] [ERROR] Exception in worker process:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
这是 Gunicorn 中的错误吗?
想通了。 resource
一词显然是 Gunicorn 的某种名称冲突。必须重命名我的应用程序和所有对 resources
的引用才能修复它。
等等...