Is virtualenv recommended for django production server?

PythonDjangoDeployment

Python Problem Overview


I have always been using virtualenv for testing my app in localhost since I have isolated environment and can safely test new release of packages.

Now It comes the time when I have to deploy my app to a production server. I am wondering if I should also use virtualenv for production server or just normal installation should do. Since it's production server I can always use the correct version that I tested in the dev server (under virtual-env)

Python Solutions


Solution 1 - Python

I would do it that way if you ever think you'll run more than one project on the webserver. As soon as you have two projects you run the risk of a future upgrade of any python package breaking the other site.

Solution 2 - Python

> Is virtualenv recommended for django production server?

Yes, it makes your project not depend on certain aspects of the system environment and also it allows you to make the deployment process more clear and configurable.

I use fabric, pip and virtualenv to deploy all my Django projects.

Solution 3 - Python

Yes, I think you should use virtualenv to deploy it into production. It makes things a lot easier and cleaner for you, especially if you plan on deploying multiple services, e.g. django based websites or other python projects. You don't want each of them to be polluting the global python environment with their packages.

I think virtualenv will help you manage all your dependencies cleanly.

To update your production env all you need to do is to:

pip -r name_of_your_requirements_file.txt

I use virtualenvs in production, and you can use uWSGI to serve the applications, with Cherokee as a web server.

To use your virtualenv in production, you will need to add its path to your PYTHONPATH.

For example if your env has the path "/home/www/my_project/env/", the path to add would be:

/home/www/env/lib/python2.7/site-packages/

You can set this up in many different ways, but if you're generating your FCGI or uWSGI interface through manage.py, simply add the following at the very top of your manage.py (before the rest):

import os
my_virtualenv_path = "/home/www/my_project/env/lib/python2.7/site-packages/"
# Add it to your PYTHONPATH
os.path.append(my_virtualenv_path)

You can adapt this to your setup, just in case you could also do the following in the shell:

export PYTHONPATH:$PYTHONPATH:/home/www/my_project/env/lib/python2.7/site-packages/

You will also need to add the directory that contains your settings.py file to the PYTHONPATH, so Django will be able to discover it. Just proceed in a similar manner to do so.

Solution 4 - Python

In most cases I would agree it is best to have a virtualenv even if it doesn't seem like you need it when you first set up the server. That said if you are using some kind of cloud service and spinning up servers for a specific task for a short time then I don't see the point of using a virtualenv.

Solution 5 - Python

I think its a good indication that its a fully supported production solution when uwsgi directly supports it with the vhost flag: http://projects.unbit.it/uwsgi/wiki/VirtualHosting

Solution 6 - Python

Using Docker containers for both development and production deployment is pretty popular now, so if you're considering following this trend, you won't need virtualenv anymore.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionw00dView Question on Stackoverflow
Solution 1 - PythonKurtView Answer on Stackoverflow
Solution 2 - PythonStack Exchange UserView Answer on Stackoverflow
Solution 3 - PythonAaronOView Answer on Stackoverflow
Solution 4 - PythonMax PetersonView Answer on Stackoverflow
Solution 5 - PythonjdiView Answer on Stackoverflow
Solution 6 - PythonDmitrii MikhailovView Answer on Stackoverflow