简体   繁体   中英

Gunicorn.service unable to read Python3.6 library

I am deploying a flask app on nginx+gunicorn on ubuntu server.

Gunicorn command runs the flask app properly. The following is the command

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

When I try to write a service it fails with the error

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'.

The error occurs when app runs through python2.7 instead of 3.6.

The Service file /etc/systemd/system/gunicorn3.service is

[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

The app runs properly when I run a direct gunicorn3 command but fails when service is created

Your problem seems to be that your gunicorn command is pointing to different Python versions.


The app runs properly when I run a direct gunicorn3 command but fails when service is created

Try:

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

More clean solution.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM