【问题标题】:How to get Gunicorn to use Python 3 instead of Python 2 (502 Bad Gateway)如何让 Gunicorn 使用 Python 3 而不是 Python 2(502 Bad Gateway)
【发布时间】:2014-07-18 15:44:54
【问题描述】:

我正在尝试让 Gunicorn 将 Python3 用于我想要制作的 Django 应用程序。我正在使用 Digital Ocean 的 Django 图像开始。它附带安装和配置的 Django、Gunicorn 和 Nginx。此图像附带的默认 Django 项目似乎适用于 Python 2。

我已经apt-get'ed 这些包。

  • python3
  • python3-psycopg2
  • python3-dev
  • python3-pip

为了尽量避免出现任何问题,我也这样做了。

  • pip 卸载 django
  • pip3 安装 django

rm -rf'ed 了股票项目并使用django-admin.py startproject django_project 创建了一个新项目。 django-admin.py 使用 Python 3(根据 shebang)。后来我用python3 manage.py startapp django_app新建了一个app。

此时,一切正常。就像默认应用程序一样。然后,在django_app/views.py 我这样做了,它就坏了。

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    # Python 2 and 3 - works fine
    # print('PRINTING')

    # Python 3 only - crashes
    print(1, 2, end=' ')

    return HttpResponse("Hello, world! This is my first view.")

错误页面显示我使用的是 Python 2.7.6。

好的,所以我想我可以通过 pip 为 Python 3 安装 Gunicorn,所以我这样做了。

  • pip 卸载 gunicorn
  • pip3 安装 gunicorn

但后来我得到了 502 Bad Gateway。当我执行service gunicorn status 时,我得到gunicorn stop/waiting。我试过service gunicorn restart,但它仍然显示gunicorn stop/waiting

我做了一个which gunicorn,它安装在/usr/local/bin/gunicorn。呃......我不确定我还能尝试什么。任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: django python-2.7 python-3.x nginx gunicorn


    【解决方案1】:

    似乎有一个名为 gunicorn3 的包(这是在 ubuntu 上测试的)

    sudo apt-get install gunicorn3

    然后运行以下命令应该可以使用 python3 运行 gunicorn:

    gunicorn3 --log-level debug --bind 0.0.0.0:30443 server:app

    【讨论】:

    • 这是唯一适用于 Ubuntu,python 3.6 的答案。谢谢。
    • 这是一个开始,但不幸的是还不够:它从 Python 3.6 开始,但我需要 Python 3.7。想要避免虚拟环境。
    • 这是在使用 digitalocean django 图像启动 droplet 后替代 gunicorn 的。不敢相信他们仍然默认使用 python2。
    【解决方案2】:

    万一这两个链接有一天断开了,这就是我的工作方式。

    执行这些指令后开始。

    • pip uninstall gunicorn
    • pip3 install gunicorn

    安装supervisorsudo apt-get install supervisor

    接下来,我需要在我的项目目录的根目录中创建gunicorn_config.py,其中包含这个。

    command = '/usr/local/bin/gunicorn'
    pythonpath = '/home/django/django_project'
    bind = '127.0.0.1:9000'
    workers = 3
    user = 'nobody'
    

    然后,我为supervisor 创建了一个配置文件。 vim /etc/supervisor/conf.d/gunicorn.conf,有这些内容。

    [program:gunicorn]
    command=/usr/local/bin/gunicorn -c /home/django/django_project/gunicorn_config.py django_project.wsgi
    user=nobody
    autostart=true
    autorestart=true
    stderr_logfile=/var/log/gunicorn3.err.log
    stdout_logfile=/var/log/gunicorn3.out.log
    

    在那之后,我做了supervisorctl rereadsupervisorctl update 然后一切都开始工作了。

    您可以使用supervisorctl status gunicorn 来检查gunicorn 是否正在运行。可以使用supervisorctl restart gunicorn重启。

    【讨论】:

    • 这就像一个魅力:) 非常感谢。在花费数小时后,您的解决方案奏效了。
    【解决方案3】:

    我的方式:

    virtualenv -p /usr/bin/python3 /home/py3env
    source /home/py3env/bin/activate
    pip3 install gunicorn
    /home/py3env/bin/gunicorn -w4 -b0.0.0.0:8000 [projectname].wsgi
    

    【讨论】:

    • 这个答案最能反映如何解决 Digital Ocean 教程中的问题
    • 奇怪的是,即使which gunicorn 在我的 virtualenv 中输出 gunicorn,命令 gunicorn myapp 由于缺少库而失败(换句话说,它使用系统 python 而不是虚拟 env).. . 但是命令$(which gunicorn) myapp 工作正常。
    【解决方案4】:

    重新开始可能更容易。教程https://www.digitalocean.com/community/articles/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn

    我让它在一个新的 ubuntu 14.04 droplet 上运行。安装 python3 和 django,然后按照教程进行操作。虽然没有做 postgres 或 virtualenv 位。

    【讨论】:

    • 好的,从Digital Ocean教程中的“第九步:配置Gunicorn”开始工作。但是,我必须使用/usr/local/bin/gunicorn -c /home/django/django_project/gunicorn_config.py django_project.wsgi 手动启动服务器。此外,这不会与service gunicorn 挂钩。
    • 好吧,请继续阅读:'如果您的 VPS 重新启动或者它由于某种原因崩溃,这也带来了需要手动启动或重新启动 Gunicorn 的问题。为了解决这个问题,大多数人使用 supervisord 来管理 Gunicorn 并根据需要启动/重启它。安装和配置 supervisord 已在另一篇文章中介绍,可在此处找到:digitalocean.com/community/articles/…。'
    • 在您卸载/重新安装 gunicorn 后,您似乎可以开始正确使用 python3,但以防万一出现问题!尝试运行which gunicorn -a。对我来说,我不得不使用类似 /opt/python-3.4.2/bin/gunicorn 的东西(在 CentOS 中)。如果这不相关,则忽略它!
    【解决方案5】:

    编写以下脚本以使用 DigitalOcean 的 14.04 Django 映像切换到 Python 3.4,因为我希望它是一个不错的一步设置...它将在 https://gist.github.com/tr00st/190ab4de62f9b23bea69 维护

    对我来说,设置的主要问题是 gevent,切换到 tornado for workers 工作正常。

    #!/bin/bash
    # Python 3 Upgrade for Django Droplet
    # Will update the "Django on 14.04" Digital Ocean image.
    # Run as root.
    
    # Grab psycopg2 and pip
    apt-get install python3-pip python3-psycopg2
    
    # Remove the Python 2.7 version of gunicorn, so we can...
    pip uninstall gunicorn
    
    # Install the Python 3 version of gunicorn, and a couple of dependencies.
    pip3 install gunicorn tornado django
    # Sadly, at time of writing, gevent isn't Python 3 compatible... But tornado is!
    # So, switch them out with a little sed magic
    sed 's/worker_class = '\''gevent'\''/worker_class='\''tornado'\''/' /etc/gunicorn.d/gunicorn.py -i.orig
    
    # Restart gunicorn to make the changes take effect...
    service gunicorn restart
    
    # And we're good!
    

    【讨论】:

      【解决方案6】:

      我实现它的方法是从任何地方卸载 gunicorn:

      sudo apt-get remove gunicorn
      pip uninstall gunicorn
      pip3 uninstall gunicorn
      

      然后安装gunicornfrom source

      pip3 install git+https://github.com/benoitc/gunicorn.git
      

      现在一切正常。

      【讨论】:

      • 这是在 OSX 上对我有用的唯一答案(跳过了 apt-get 部分)谢谢
      【解决方案7】:

      我认为最好的方法是转到 /usr/local/bin/gunicorn 并将第一行(即 shebang 行)更改为 #!/usr/bin/python-version

      例如: 我的 gunicorn 使用 python3.5 解释器运行

      #!/usr/bin/python3.5
      
      # -*- coding: utf-8 -*-
      import re
      import sys
      
      from gunicorn.app.wsgiapp import run
      
      if __name__ == '__main__':
          sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
          sys.exit(run())
      

      【讨论】:

        【解决方案8】:

        如果您查看gunicorn 可执行文件,它只是一个小的python 脚本:

        $ cat gunicorn
        #!/usr/bin/env python
        
        # -*- coding: utf-8 -*-
        import re
        import sys
        
        from gunicorn.app.wsgiapp import run
        
        if __name__ == '__main__':
            sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
            sys.exit(run())
        

        重要的一点是from gunicorn.app.wsgiapp import run 行,它告诉您负责运行您的应用程序的模块的位置。如果 gunicorn 是高质量的代码(确实如此),您应该能够直接从命令行导入该模块并运行您的应用程序。

        “直接从命令行导入模块”表示使用-m command line switch

        当使用 -m module-name 调用时,给定模块位于 Python 模块路径上并作为脚本执行。

        使用您选择的python:

        $ /path/to/your/python -m gunicorn.app.wsgiapp {{ gunicorn command line args go here }} server:app
        

        果然它运行了!

        [2017-12-04 02:05:27 +0000] [24] [INFO] Starting gunicorn 19.7.1
        [2017-12-04 02:05:27 +0000] [24] [INFO] Listening at: http://127.0.0.1:5000 (24)
        [2017-12-04 02:05:27 +0000] [24] [INFO] Using worker: sync
        [2017-12-04 02:05:27 +0000] [28] [INFO] Booting worker with pid: 28
        [2017-12-04 02:05:27 +0000] [29] [INFO] Booting worker with pid: 29
        

        这种行为非常有用,例如在从 Dockerfile 之类的地方运行 gunicorn 时。

        【讨论】:

          【解决方案9】:

          在你的项目文件夹中安装一个 python3 虚拟环境

          $ pipenv --three
          

          然后在激活的环境中运行gunicorn

          $ gunicorn [PROJECT].wgsi
          

          【讨论】:

            【解决方案10】:

            你可以使用虚拟环境

            python3 -m venv .venv
            source .venv/bin/activate
            pip install gunicorn
            

            并且,对于启动 gunicorn(如果您已经在全球范围内)

            .venv/bin/gunicorn # instead of gunicorn
            

            【讨论】:

              【解决方案11】:

              我在 Digital Ocean 上使用 droplet“Ubuntu Django on 14.04”时遇到了同样的问题。

              我意识到在使用 Python 3 时“gevent”工作器类型对我来说是个问题。即使我与python3 -m pip freeze 确认安装了“gevent”,它也没有工作。我在 /etc/gunicorn.d/gunicorn.py 中将其更改为“同步”:

              ...
              worker_class = 'sync'
              ...
              

              我重新启动了 gunicorn:

              sudo service gunicorn restart
              

              我使用service gunicorn status检查了gunicorn服务是否正在运行,并且能够通过访问我的Droplet的IP地址看到欢迎来到django页面。

              我希望这对其他人有用。

              【讨论】:

                【解决方案12】:

                这就是我如何让它为我工作。我已经用 pip2 和 pip3 安装了 gunicorn。我需要两个版本。默认是带有 pip2 的 gunicorn。

                我所做的是我使用 pip3 安装了带有 virtualenv 的 gunicorn,并查看了 virtualenv 文件夹中 bin 下的 gunicorn 文件的内容,即

                #!/********/virtualenv/gunicorn3/corto/bin/python3
                
                # -*- coding: utf-8 -*-
                import re
                import sys
                
                from gunicorn.app.wsgiapp import run
                
                if __name__ == '__main__':
                    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
                    sys.exit(run())
                

                我复制了这个文件并把它放在别处,然后我改变了

                #!/********/virtualenv/gunicorn3/corto/bin/python3
                

                #!/usr/bin/python3
                

                现在你可以在进入你复制gunicorn文件的目录后像这样轻松运行gunicorn

                python3 gunicorn -c /your_config_file.py class_app:app
                

                注意:执行 pip3 uninstall gunicorn,然后 pip3 install gunicorn 将在 dir 'usr/local/bin' (ubuntu) 中安装具有 python3 版本(覆盖 python2 版本)的 gunicron,然后您可以获取 gunicron 的文件内容那里。这将帮助您避免使用 virtualenv。

                如果这第一次不起作用,请执行 pip3 unistall gunicorn,然后执行 pip3 install gunicorn。

                独角兽快乐 ;)

                【讨论】:

                  猜你喜欢
                  • 2023-03-20
                  • 2018-08-03
                  • 2017-04-13
                  • 1970-01-01
                  • 2016-04-20
                  • 2020-09-01
                  • 1970-01-01
                  • 2017-03-18
                  • 2020-06-12
                  相关资源
                  最近更新 更多