简体   繁体   English

gunicorn 无法启动 flask 服务器:ModuleNotFoundError:没有名为“请求”的模块

[英]gunicorn fails to launch flask server: ModuleNotFoundError: No module named 'requests'

Flask server runs perfectly fine but when running it with gunicorn, it shows the following error: Flask 服务器运行良好,但使用 gunicorn 运行时,显示以下错误:

[2020-07-08 14:58:33 +0000] [27561] [INFO] Starting gunicorn 20.0.4
[2020-07-08 14:58:33 +0000] [27561] [INFO] Listening at: http://172.20.31.202:5000 (27561)
[2020-07-08 14:58:33 +0000] [27561] [INFO] Using worker: sync
[2020-07-08 14:58:33 +0000] [27564] [INFO] Booting worker with pid: 27564
[2020-07-08 14:58:34 +0000] [27564] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
...
...
  File "/root/customer-account-automation/wsgi.py", line 7, in <module>
    from app import app as application
  File "/root/customer-account-automation/app.py", line 6, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'
[2020-07-08 14:58:34 +0000] [27564] [INFO] Worker exiting (pid: 27564)
[2020-07-08 14:58:34 +0000] [27561] [INFO] Shutting down: Master
[2020-07-08 14:58:34 +0000] [27561] [INFO] Reason: Worker failed to boot.

I am not doubting the package installation because it already works with flask run .我不怀疑 package 安装,因为它已经与flask run一起使用。

This is the wsgi.py code这是wsgi.py代码

#!/usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)

from app import app as application
if __name == "__main__":
    application.run()

and the pipenv site-packages directory contains both requests and gunicorn as shown below: pipenv site-packages 目录包含requestsgunicorn ,如下所示:

(customer-account-automation) root@jsd-user-management:~/customer-account-automation# ll /root/.local/share/virtualenvs/customer-account-automation-gLS21FFx/lib/python3.7/site-packages/
...
drwxr-xr-x  7 root root   4096 Jul  8 15:14 gunicorn/
drwxr-xr-x  2 root root   4096 Jul  8 15:14 gunicorn-20.0.4.dist-info/
drwxr-xr-x  3 root root   4096 Jul  8 14:39 requests/
drwxr-xr-x  2 root root   4096 Jul  8 14:39 requests-2.24.0.dist-info/
drwxr-xr-x  4 root root   4096 Jul  8 14:39 requests_oauthlib/
drwxr-xr-x  2 root root   4096 Jul  8 14:39 requests_oauthlib-1.3.0.dist-info/
drwxr-xr-x 10 root root   4096 Jul  8 14:39 requests_toolbelt/
drwxr-xr-x  2 root root   4096 Jul  8 14:39 requests_toolbelt-0.9.1.dist-info/
...

What could be there that gunicorn fails to import Requests but flask run succeeds? gunicorn 无法导入请求但flask run成功的原因是什么? Could it be related to the pipenv?它可能与pipenv有关吗?

It looks like the error is coming from a worker process.看起来错误来自工作进程。 These will probably be opening subprocesses in python and may be calling the wrong version of python on your system - a version that doesn't have requests installed.这些可能会在 python 中打开子进程,并且可能会在您的系统上调用错误版本的 python - 一个没有安装请求的版本。

The config of gunicorn allows you to specify additions to the python path at runtime. gunicorn 的配置允许您在运行时指定添加到 python 路径。

class PythonPath(Setting):
    name = "pythonpath"
    section = "Server Mechanics"
    cli = ["--pythonpath"]
    meta = "STRING"
    validator = validate_string
    default = None
    desc = """\
        A comma-separated list of directories to add to the Python path.
        e.g.
        ``'/home/djangoprojects/myproject,/home/python/mylibrary'``.
        """

You could try adding the location of the requests module when you run gunicorn.您可以在运行 gunicorn 时尝试添加 requests 模块的位置。 You can find where requests is by running pip show requests .您可以通过运行pip show requests找到请求的位置。

> pip show requests
Name: requests
Version: 2.23.0
...
Location: /root/.local/share/virtualenvs/customer-account-automation-gLS21FFx/lib/python3.7/site-packages/

Then append the location to whatever command you use for gunicorn:然后 append 的位置到您用于 gunicorn 的任何命令:

gunicorn --pythonpath /root/.local/share/virtualenvs/customer-account-automation-gLS21FFx/lib/python3.7/site-packages/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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