python - 无法使用类似 gunicorn 的服务运行 Flask 应用程序

标签 python linux flask service gunicorn

我正在尝试通过使用 Gunicorn 运行我的 Flask 应用程序来提供服务。 服务文件如下所示:

[Unit]
Description=metrofind
After=network.target

[Service]
User=ubuntu
Group=ubuntu
Restart=on-failure
WorkingDirectory=/home/ubuntu/geoMetroFinding/
ExecStart=/home/ubuntu/geoMetroFinding/flaskenv/bin/gunicorn -c /home/ubuntu/geoMetroFinding/gunicorn.conf -b 0.0.0.0:5000 main_flask:app

[Install]
WantedBy=multi-user.target

在它写入的日志错误文件中:

ImportError: No module named 'pandas'

ImportError: No module named 'main_flask'

但在虚拟环境中,所有必需的包都已安装。

当我在控制台中从目录“geoMetroFinding”在本地运行此命令时:

  gunicorn -c gunicorn.conf -b 0.0.0.0:5000 main_flask:app

应用正在运行

怎么了?

最佳答案

您必须在服务文件中设置环境。

[服务] 部分的示例如下:

[Service]
User=ubuntu
Group=ubuntu
Restart=on-failure
WorkingDirectory=/home/ubuntu/geoMetroFinding/
Environment="PATH=/home/ubuntu/geoMetroFinding/flaskenv/bin"
ExecStart=/home/ubuntu/geoMetroFinding/flaskenv/bin/gunicorn -c /home/ubuntu/geoMetroFinding/gunicorn.conf -b 0.0.0.0:5000 main_flask:app

因此您需要将环境添加到您的 PATH 中才能使其正常工作。

看看this great tutorial了解更多信息。

关于python - 无法使用类似 gunicorn 的服务运行 Flask 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45797066/

相关文章:

python - 有没有更简单的方法来转换这些 python 对象

python - 使用 pandas 找出一组的垃圾箱百分比

python - 为什么无法在浏览器中访问正在运行的 Docker 容器?

linux - Oracle Linux 服务器上的 phpmyadmin

python - 如何让 Flask_Security 中的 auth_token_required 工作?

python - 将Python输入转化为命令

linux - 在 bash 中读取时转义规则

linux - find - 我需要从 RPM 列表中获取包的名称

javascript - 如何在flask中处理请求json数据

python - 重复 Dialogflow 意图直到满足条件