Module installed but "ModuleNotFoundError: No module named <module_name>" while running gunicorn
Module installed but "ModuleNotFoundError: No module named <module_name>" while running gunicorn
我正在尝试使用 nginx 部署此网站。该站点配置正确,这已在我 运行 python manage.py runserver
时得到验证。但是当我 运行 同样使用 gunicorn 和 wsgi 时,gunicorn --bind 0.0.0.0:8002 config.wsgi
它会显示错误:ModuleNotFoundError: No module named 'crispy_forms'
.
Crispy_forms 已安装并已使用 pip freeze
进行验证。还启用了虚拟环境。 'crispy-forms' 也已添加到 settings.py
中的 INSTALLED_APPS 中。
以下是我的错误代码:
root@devvm:/tmp/tmc_site# gunicorn --bind 0.0.0.0:8002 config.wsgi
[2022-02-03 23:03:07 +0000] [6333] [INFO] Starting gunicorn 20.1.0
[2022-02-03 23:03:07 +0000] [6333] [INFO] Listening at: http://0.0.0.0:8002 (6333)
[2022-02-03 23:03:07 +0000] [6333] [INFO] Using worker: sync
[2022-02-03 23:03:07 +0000] [6335] [INFO] Booting worker with pid: 6335
[2022-02-04 04:03:07 +0500] [6335] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/gunicorn/arbiter.py", line 589, in spawn_worker
worker.init_process()
File "/usr/local/lib/python3.8/dist-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/usr/local/lib/python3.8/dist-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/local/lib/python3.8/dist-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/local/lib/python3.8/dist-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/usr/local/lib/python3.8/dist-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/local/lib/python3.8/dist-packages/gunicorn/util.py", line 359, in import_app
mod = importlib.import_module(module)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/tmp/tmc_site/config/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/usr/local/lib/python3.8/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "/usr/local/lib/python3.8/dist-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.8/dist-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python3.8/dist-packages/django/apps/config.py", line 223, in create
import_module(entry)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'crispy_forms'
[2022-02-04 04:03:07 +0500] [6335] [INFO] Worker exiting (pid: 6335)
[2022-02-03 23:03:07 +0000] [6333] [INFO] Shutting down: Master
[2022-02-03 23:03:07 +0000] [6333] [INFO] Reason: Worker failed to boot.
这是我的项目结构,我已将配置文件的文件夹名称更改为“config”。
tmc_site
-- .
-- ..
-- assets
-- config
-- asgi.py
-- __init__.py
-- __pycache__
-- settings.py
-- urls.py
-- wsgi.py
-- db.json
-- db.sqlite3
- .git
-- .gitattributes
-- .gitignore
-- manage.py
-- requirements.txt
-- static
-- venv
-- website
以下是我的wsgi相关文件内容:
settings.py: WSGI_APPLICATION = 'config.wsgi.application'
wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
application = get_wsgi_application()
如有任何帮助,我们将不胜感激。
从报错信息可以看出,系统安装的gunicorn尝试使用/usr/local/lib/python3.8/dist-packages/
中的全局Python环境,而不是/tmp/tmc_site/venv
目录中的项目特定虚拟环境。
在 venv
虚拟环境中安装 gunicorn,因此在您激活它之后,gunicorn --bind 0.0.0.0:8002 config.wsgi
命令将使用具有正确环境的本地环境。您也可以通过 ./venv/bin/gunicorn
.
直接从虚拟环境调用 gunicorn
我正在尝试使用 nginx 部署此网站。该站点配置正确,这已在我 运行 python manage.py runserver
时得到验证。但是当我 运行 同样使用 gunicorn 和 wsgi 时,gunicorn --bind 0.0.0.0:8002 config.wsgi
它会显示错误:ModuleNotFoundError: No module named 'crispy_forms'
.
Crispy_forms 已安装并已使用 pip freeze
进行验证。还启用了虚拟环境。 'crispy-forms' 也已添加到 settings.py
中的 INSTALLED_APPS 中。
以下是我的错误代码:
root@devvm:/tmp/tmc_site# gunicorn --bind 0.0.0.0:8002 config.wsgi
[2022-02-03 23:03:07 +0000] [6333] [INFO] Starting gunicorn 20.1.0
[2022-02-03 23:03:07 +0000] [6333] [INFO] Listening at: http://0.0.0.0:8002 (6333)
[2022-02-03 23:03:07 +0000] [6333] [INFO] Using worker: sync
[2022-02-03 23:03:07 +0000] [6335] [INFO] Booting worker with pid: 6335
[2022-02-04 04:03:07 +0500] [6335] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/gunicorn/arbiter.py", line 589, in spawn_worker
worker.init_process()
File "/usr/local/lib/python3.8/dist-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/usr/local/lib/python3.8/dist-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/local/lib/python3.8/dist-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/local/lib/python3.8/dist-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/usr/local/lib/python3.8/dist-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/local/lib/python3.8/dist-packages/gunicorn/util.py", line 359, in import_app
mod = importlib.import_module(module)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/tmp/tmc_site/config/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/usr/local/lib/python3.8/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "/usr/local/lib/python3.8/dist-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.8/dist-packages/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/usr/local/lib/python3.8/dist-packages/django/apps/config.py", line 223, in create
import_module(entry)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'crispy_forms'
[2022-02-04 04:03:07 +0500] [6335] [INFO] Worker exiting (pid: 6335)
[2022-02-03 23:03:07 +0000] [6333] [INFO] Shutting down: Master
[2022-02-03 23:03:07 +0000] [6333] [INFO] Reason: Worker failed to boot.
这是我的项目结构,我已将配置文件的文件夹名称更改为“config”。
tmc_site
-- .
-- ..
-- assets
-- config
-- asgi.py
-- __init__.py
-- __pycache__
-- settings.py
-- urls.py
-- wsgi.py
-- db.json
-- db.sqlite3
- .git
-- .gitattributes
-- .gitignore
-- manage.py
-- requirements.txt
-- static
-- venv
-- website
以下是我的wsgi相关文件内容:
settings.py: WSGI_APPLICATION = 'config.wsgi.application'
wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
application = get_wsgi_application()
如有任何帮助,我们将不胜感激。
从报错信息可以看出,系统安装的gunicorn尝试使用/usr/local/lib/python3.8/dist-packages/
中的全局Python环境,而不是/tmp/tmc_site/venv
目录中的项目特定虚拟环境。
在 venv
虚拟环境中安装 gunicorn,因此在您激活它之后,gunicorn --bind 0.0.0.0:8002 config.wsgi
命令将使用具有正确环境的本地环境。您也可以通过 ./venv/bin/gunicorn
.