【问题标题】:Cannot get gunicorn to use Python 3无法让 gunicorn 使用 Python 3
【发布时间】:2015-02-19 18:03:52
【问题描述】:

我有 Ubuntu+NGINX+Gunicorn 和一个虚拟环境,可与 Python 3 设置一起使用,但我的 Flask 应用程序仍以 2.7.6 运行。我有条不紊地按照说明进行操作,但找不到解决方法。

Gunicorn 配置文件

[program:app-server]
command = gunicorn app:app -b localhost:8000
directory = /home/www/app-server
user = appuser

项目目录结构

app-server    
----app.py
----venv (virtual environment)

【问题讨论】:

  • 这看起来更像是一个supervisord配置而不是gunicorn配置文件。
  • 您需要使用pip3(或有时pip3.4)安装它。所以运行pip uninstall gunicorn(如果你不需要python 2.6/2.7 版本),然后运行pip3 install gunicorn。如果您忽略了该卸载部分,或者因为它不是您的服务器而不允许这样做,请不要担心,只需运行 which gunicorn -a。您使用的可能不是正确的。之前(在 CentOS 上)我不得不使用类似 /opt/python-3.4.2/bin/gunicorn 的东西。我知道你说的是 Ubuntu,但你永远不知道!

标签: python-3.x flask gunicorn


【解决方案1】:

Gunicorn 本身就是一个 Python 应用程序; Gunicorn PPA 仅发布 Python 2 版本。

您可以将 Gunicorn 安装到 Python 3 中(使用 pip,最好安装到您的 virtualenv 中)并运行该版本:

/home/www/app-server/venv/bin/gunicorn

【讨论】:

  • 无论我做什么,这对我都不起作用,我感到非常难过。在本地它可以正常工作,但是当我部署它时却没有。
  • @CatManDo: 不起作用 并不是什么问题描述。您可以手动在远程服务器上启动 Gunicorn 服务器(virtualenv 中的那个)吗?如果没有,会发生什么?等等。
  • 我使用 NGINX 作为反向代理,但是如果我使用命令行 gunicorn app:app -b localhost:8000 (正在使用的端口),我的应用程序仍会打印出 2.7.6 作为 Python 版本当我调用 sys.version.
  • @CatManDo: 但只是简单的gunicorn 运行系统安装的 Gunicorn,而不是安装在您的 virtualenv 中的那个。
【解决方案2】:

虽然这个问题已经有一年多了,但我想我应该添加我的答案,以防有人来搜索

用文本编辑器打开这个文件“/usr/bin/gunicorn”。就我而言,我在其他情况下使用 vim 和 sudo 来编辑文件

然后将python版本改为python3 usr/bin/python2.7 将是 usr/bin/python3

然后将文件中的版本号更改为安装在虚拟环境中的gunicorn的版本号。如果两者不相同,则为目录。 在我的情况下,文件中的版本号是 17.0,但我拥有的是 19.6.0

请注意,文件中有两个地方可以更改版本号。

    #! /usr/bin/python3
    # EASY-INSTALL-ENTRY SCRIPT'gunicorn==17.5','console_scripts','gunicorn'
    __requires__ = 'gunicorn==19.6.0'
    import sys
    from pkg_resources import load_entry_point

    if __name__ == '__main__':
        sys.exit(
                  load_entry_point('gunicorn==19.6.0', console_scripts, 
                   'gunicorn')
                )

【讨论】:

  • 如果要运行venv中安装的gunicorn,应该在venv中运行gunicorn,不要让系统安装的gunicorn看起来像venv版本。
  • @NikolayFrick 这不是一个需要编辑库文件的优雅解决方案。
猜你喜欢
  • 2014-07-18
  • 1970-01-01
  • 2022-12-25
  • 2021-08-11
  • 1970-01-01
  • 2016-05-23
  • 2013-10-08
  • 1970-01-01
  • 2013-02-15
相关资源
最近更新 更多