【问题标题】:Gunicorn.service unable to read Python3.6 libraryGunicorn.service 无法读取 Python3.6 库
【发布时间】:2020-09-21 21:00:34
【问题描述】:

我正在 ubuntu 服务器上的 nginx+gunicorn 上部署一个烧瓶应用程序。

Gunicorn 命令正确运行烧瓶应用程序。以下是命令

gunicorn3 --bind=unix:/tmp/gunicorn.sock --workers=4 app:app

当我尝试编写服务时,它失败并出现错误

gunicorn3.service - gunicorn3 daemon for /var/www/html/my_app/app.py
   Loaded: loaded (/etc/systemd/system/gunicorn3.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2020-06-03 13:26:57 IST; 6s ago
  Process: 19575 ExecStart=/usr/bin/gunicorn3 --bind=unix:/tmp/gunicorn3.sock --workers=4 app:app (code=exited, status=3)
 Main PID: 19575 (code=exited, status=3)

Jun 03 13:26:57 my-server gunicorn3[19575]:   File "/usr/lib/python3/dist-packages/gunicorn/util.py", line 377, in import_app
Jun 03 13:26:57 my-server gunicorn3[19575]:     _import_(module)
Jun 03 13:26:57 my-server gunicorn3[19575]:   File "/var/www/html/my_app/app.py", line 1, in <module>
Jun 03 13:26:57 my-server gunicorn3[19575]:     from flask import Flask, render_template, flash, request, url_for, redirect, session, flash, g, jsonify, send_file, make_response, send_from_directory
Jun 03 13:26:57 my-server gunicorn3[19575]: ModuleNotFoundError: No module named 'flask'
Jun 03 13:26:57 my-server gunicorn3[19575]: [2020-06-03 13:26:57 +0530] [19577] [INFO] Worker exiting (pid: 19577)
Jun 03 13:26:57 my-server gunicorn3[19575]: [2020-06-03 13:26:57 +0530] [19575] [INFO] Shutting down: Master
Jun 03 13:26:57 my-server gunicorn3[19575]: [2020-06-03 13:26:57 +0530] [19575] [INFO] Reason: Worker failed to boot.
Jun 03 13:26:57 my-server systemd[1]: gunicorn3.service: Main process exited, code=exited, status=3/NOTIMPLEMENTED
Jun 03 13:26:57 my-server systemd[1]: gunicorn3.service: Failed with result 'exit-code'.

当app运行python2.7而不是3.6时出现错误。

Service 文件 /etc/systemd/system/gunicorn3.service 是

[Unit]
Description=gunicorn3 daemon for /var/www/html/my_app/app.py
After=network.target

[Service]
User=root
Group=www-data
RuntimeDirectory=gunicorn3
WorkingDirectory=/var/www/html/my_app/
ExecStart=/usr/bin/gunicorn3 --bind=unix:/tmp/gunicorn3.sock --workers=4 app:app
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

当我运行直接 gunicorn3 命令时应用程序运行正常,但在创建服务时失败

【问题讨论】:

    标签: python linux nginx flask gunicorn


    【解决方案1】:

    您的问题似乎是您的 gunicorn 命令指向不同的 Python 版本。


    当我运行直接 gunicorn3 命令但失败时,应用程序运行正常 何时创建服务

    试试:

    ExecStart=/usr/bin/env gunicorn3 --bind=unix:/tmp/gunicorn3.sock --workers=4 app:app
    

    更干净的解决方案。

    python3 -m venv venv
    source venv/bin/activate
    python -m pip install -r requirements.txt
    
    ExecStart=/path/to/your/venv/bin/gunicorn --bind=unix:/tmp/gunicorn3.sock --workers=4 app:app
    

    【讨论】:

    • 有必要做虚拟环境吗?
    • 这不是必需的,但强烈推荐。您可以在没有虚拟环境的情况下轻松破坏正在运行的系统。
    猜你喜欢
    • 2018-02-11
    • 2019-05-07
    • 2019-09-14
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2018-09-22
    • 1970-01-01
    相关资源
    最近更新 更多