Run Django Python 2 and Python 3 apps with uWSGI and nginx on same server?

How do I run multiple django apps using uwsgi and nginx on ubuntu 14.04 where one of them is on python 3 and the rest are on python 2?

This was originally asked by kimstacks in reference to the tutorial: “How To Serve Django Applications with uWSGI and Nginx on Ubuntu 14.04” - But I thought it would be useful to others so I reposted the Q&A here

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

You will probably have to have multiple emperor processes in this case. Usually, uWSGI is built against a particular version of Python. However, it is apparently possible to “stack” emperors so that a master emperor process can call individual Python version-specific emperor processes, which can then spawn your Django projects. I see that you’ve already found this StackOverflow thread on the subject.

I just tried this for the first time personally and was able to get it to work. The process is still very new to me, but I’ll try to explain.

Start off by going through the tutorial as usual. For your Python 3 site, you’ll want to create the virtual environment like this:

  1. mkvirtualenv --python=/usr/bin/python3 python3projectname

If you run through the guide, you will have uWSGI built against Python 2 installed, and will be able to serve your Python 2 sites. To get the Python3 version working, you’ll have to first install the development files through apt:

  1. sudo apt-get update
  2. sudo apt-get install python3-dev

Make a new virtual environment for the Python 3 version of uWSGI (you could just install this in your Python 3 virtual environment, but this allows for a more explicit separation so that it’s obvious that it could run any number of Python 3 sites):

  1. mkvirtualenv --python=/usr/bin/python3 python3uwsgi

Within this new virtual environment, install uWSGI. This will be built against Python 3:

  1. pip install uwsgi

Deactivate the virtual environment when you are finished:

  1. deactivate

The next change will be in the uWSGI structure. Since the system’s uWSGI is built against Python 2, you can keep the Python 2 sites in the /etc/uwsgi/sites directory as normal. Make a new directory that will hold all of the uWSGI configs for your Python 3 sites:

  1. sudo mkdir /etc/uwsgi/sites_python3

Move the uWSGI configurations for the Python 3 site(s) into this new directory:

  1. sudo mv /etc/uwsgi/sites/python3projectname.ini /etc/uwsgi/sites_python3

We then need to add an additional uWSGI configuration file to the /etc/uwsgi/sites directory.

  1. sudo nano /etc/uwsgi/sites/spawn_python3.ini

This .ini file will spawn another uWSGI emperor process, this time using the Python 3 version of uWSGI that we installed in the virtual environment. This emperor process will read the uWSGI configuration files within the /etc/uwsgi/sites_python3 directory:

project = python3uwsgi
base = /home/user

privileged-binary-patch-arg = %(base)/Env/%(project)/bin/uwsgi --emperor /etc/uwsgi/sites_python3

Now, when you start the Python 2 version of uWSGI (using the upstart file from this guide), it will, in turn, spawn an additional Python 3 emperor to handle your Python 3 sites.

  1. sudo start uwsgi

I figured this out once and validated it a second time, but it’s still a completely new process for me, so take the above as a starting point. Hopefully that helps though!

Thank you, I literally spent 10 hours searching for a way to work with Python 3


I have the same scenario, where I have some django applications in python2.X and others in python3.x. I have tried the methods suggested here, but got the “— no python application found, check your startup logs for errors —” in uwsgi.log file. I have installed necessary packages as mentioned here and read some blogs as well.

Could you please help?