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.
In case the two links break one day, here's how I got it working.
Starting after executing these instructions.
pip uninstall gunicorn
pip3 install gunicorn
Install
supervisor
,sudo apt-get install supervisor
.Next, I needed to make
gunicorn_config.py
in the root of my project directory, which contains this.Then, I created a configuration file for
supervisor
.vim /etc/supervisor/conf.d/gunicorn.conf
, with these contents.After that, I did a
supervisorctl reread
andsupervisorctl update
and then it all started working.You can use
supervisorctl status gunicorn
to check ifgunicorn
is running or not. You can usesupervisorctl restart gunicorn
to restart.Here is how i made it to work for me. I have installed gunicorn both with pip2 and pip3. I need both versions. The default one is gunicorn with pip2.
What I did was I installed gunicorn with virtualenv using pip3 and looked at content of the file gunicorn under bin in the virtualenv folder, that says
I copied this file and put it elsewhere, then i changed
to
Now you can run gunicorn easily like this after getting into the directory that you copied the file gunicorn in
Note: doing pip3 uninstall gunicorn, followed by pip3 install gunicorn will install gunicron with python3 version (overriding the python2 version) in the dir 'usr/local/bin' (ubuntu) and then you can get the file contents of gunicron there. This will help you avoid using virtualenv.
If this doesnt work first time, do pip3 unistall gunicorn, followed by pip3 install gunicorn.
Happy unicorning ;)
Wrote the following script to switch to Python 3.4 with DigitalOcean's 14.04 Django image, as I wanted it to be a nice one-step setup... It will be maintained at https://gist.github.com/tr00st/190ab4de62f9b23bea69
The main issue with the setup for me was with gevent, switching to tornado for workers worked fine.
Install a python3 virtual environment in your project folder
Then run gunicorn in the activated environment
If you look at the
gunicorn
executable, it's just a small python script:The important bit is the
from gunicorn.app.wsgiapp import run
line, which tells you the location of the module responsibe for running your app. If gunicorn is quality code (which it is), you should be able to import that module directly from the command line and run your app."Importing the module directly from the command line" means using the
-m
command line switch.Using the python of your choice:
And sure enough it runs!
This behavior is very useful, for example when running gunicorn from somewhere like a Dockerfile.
It's probably easier to start afresh. Tutorial at https://www.digitalocean.com/community/articles/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn.
I got it running on a fresh ubuntu 14.04 droplet. Install python3 and django and then simply follow the tutorial. Didn't do the postgres or virtualenv bits though.