How to get Gunicorn to use Python 3 instead of Pyt

2020-05-16 23:36发布

I'm trying to get Gunicorn to use Python3 for a Django app I want to make. I'm using Digital Ocean's Django image to get started. It comes with Django, Gunicorn, and Nginx installed and configured. The default Django project that comes with this image seems to work fine for Python 2.

I've apt-get'ed these packages.

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

In order to try to avoid any problems, I've also done this.

  • pip uninstall django
  • pip3 install django

I rm -rf'ed the stock project and created a new one with django-admin.py startproject django_project. django-admin.py uses Python 3 (according to the shebang). Later, I use python3 manage.py startapp django_app to create a new app.

At this point, everything works fine. Just like the default app. Then, in django_app/views.py I do this and it breaks.

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.")

The error page says I'm using Python 2.7.6.

Okay, so then I thought I could install Gunicorn through pip for Python 3, so I do this.

  • pip uninstall gunicorn
  • pip3 install gunicorn

But then I just end up with 502 Bad Gateway. When I do service gunicorn status, I get gunicorn stop/waiting. I tried service gunicorn restart, but it still says gunicorn stop/waiting.

I did a which gunicorn and it's installed at /usr/local/bin/gunicorn. Uhh... I'm not really sure what else I could try. Any help would be greatly appreciated. Thanks.

11条回答
【Aperson】
2楼-- · 2020-05-17 00:28

It seems that there's a package for this called gunicorn3 (this was tested on ubuntu)

sudo apt-get install gunicorn3

then running the following command should work and run gunicorn with python3:

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

查看更多
唯我独甜
3楼-- · 2020-05-17 00:28

I think the best way to do this is go to /usr/local/bin/gunicorn and change first line which is shebang line to #!/usr/bin/python-version

for ex: my gunicorn runs with python3.5 interpretor

#!/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())
查看更多
姐就是有狂的资本
4楼-- · 2020-05-17 00:31

I had the same issue on Digital Ocean using the droplet "Ubuntu Django on 14.04".

I realized that the 'gevent' worker type was the problem for me when using Python 3. Even though I checked with python3 -m pip freeze that 'gevent' was installed, it did not work. I changed it to 'sync' in /etc/gunicorn.d/gunicorn.py:

...
worker_class = 'sync'
...

I restarted gunicorn:

sudo service gunicorn restart

I checked the gunicorn service was running by using service gunicorn status, and was able to see the welcome to django page by reaching my droplet's ip address.

I hope that works for other people.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-05-17 00:38

my way:

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
查看更多
唯我独甜
6楼-- · 2020-05-17 00:39

The way I make it happen was to uninstall gunicorn from everywhere:

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

And then Installing gunicorn from source.

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

Now everything's running with no problem.

查看更多
登录 后发表回答