【问题标题】:Django Virtual Environment - No module named 'gunicorn'Django 虚拟环境 - 没有名为“gunicorn”的模块
【发布时间】:2021-11-24 20:24:08
【问题描述】:

我已遵循本指南 (https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04),但目前在尝试通过 gunicorn 服务文件 (/etc/systemd/system/gunicorn.service) 运行 gunicorn 时看到以下内容:

Oct 04 11:30:22 ukgcdeploy01 gunicorn[8095]:   File "/opt/envs/automation-console-env/bin/gunicorn", line 5, in <module>
Oct 04 11:30:22 ukgcdeploy01 gunicorn[8095]:     from gunicorn.app.wsgiapp import run
Oct 04 11:30:22 ukgcdeploy01 gunicorn[8095]: ModuleNotFoundError: No module named 'gunicorn'

gunicorn.service 文件包含以下内容:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/opt/envs/automation-console-env
ExecStart=/opt/envs/automation-console-env/bin/gunicorn --timeout 600 --log-level debug --error-log /var/log/automation-console-env_error.log  --access-logfile /var/log/automation-console-env_access.log --workers 3 --bind unix:/opt/envs/automation-console-env/automation-console-env.sock django_forms.wsgi:application


[Install]
WantedBy=multi-user.target

手动运行 gunicorn 可以正常工作:

gunicorn --bind 0.0.0.0:8000 myproject.wsgi

这在我不得不将我的 Python 版本从 3.5.2 升级到 3.9 之前就可以工作,由于一些问题,我最终不得不重新创建虚拟环境,所以我认为这不一定是服务的问题文件,而是我的 Python/Gunicorn 安装。

如果有人能提供一些建议,将不胜感激:)

【问题讨论】:

    标签: python django nginx virtualenv gunicorn


    【解决方案1】:

    您似乎定义了错误的路径,或者您可能在gunicorn.service 中遇到权限问题。

    首先尝试使用systemctl enable gunicorn.service 启用该服务。如果它不起作用,请尝试以下操作:

    编辑gunicorn.service如下:

    来自:--bind unix:/opt/envs/automation-console-env/automation-console-env.sock django_forms.wsgi:application

    收件人: --bind unix:/opt/envs/automation-console-env/automation-console-env.sock django_forms.wsgi

    【讨论】:

    • 谢谢!虽然很遗憾在尝试了您的建议后我仍然得到同样的结果。
    【解决方案2】:

    automation-console-env 环境是否存在?如果是这样,gunicorn 是否安装在那里?无论哪种情况,由于它在命令行上以gunicorn ... 的身份工作,因此您应该可以使用此安装。

    which gunicorn
    

    然后更改您的 systemd 单元文件以指向此可执行文件。

    【讨论】:

    • 这么简单的答案 - 谢谢!一旦我将它更改为 gunicorn 指向的位置(/usr/bin/gunicorn),整个事情就按预期工作了。
    猜你喜欢
    • 2020-08-17
    • 2016-10-27
    • 1970-01-01
    • 2019-03-03
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多