Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices of using virtual environment for web development with Django?

This is a Django and Python and maybe just a general web development question.

what's the difference between using virtualenv vs vagrant vs virtual box and etc... ?

I'm kinda confused as to when to use which one :/ I've been using virtual env this whole time and creating new virtual environments for different projects.... Is this the right way to do it? One virtualenv per project?

I'm not really sure when and where vagrant comes into play...Am I supposed to set up vagrant and then use virtualenv?

This is probably a silly question but...if I were to be working on this project with other people. Would they too have to set up a virtual env? Just to collaborate?

Wouldn't it make more sense that we all work on our local machines and then push it into the main branch? I'm just kinda confused .... I feel like I'm doing it all wrong...

Thanks for the replies everybody!

like image 242
user805981 Avatar asked Mar 08 '13 02:03

user805981


People also ask

Should I use a virtual environment for Django?

Before installing Django, it's recommended to install Virtualenv that creates new isolated environments to isolates your Python files on a per-project basis. This will ensure that any changes made to your website won't affect other websites you're developing.

Which environment is best for Django?

The Django developer team itself recommends that you use Python virtual environments!


3 Answers

Virtualenv sets up a local sandbox for you to install Python modules into.

Vagrant is an automation tool for creating Virtual Machines.

VirtualBox is a free, open source environment for running virtual machines, like those created by Vagrant.

Virtualenv is really about all you'll need to do sandboxed development on your local machine. We use Vagrant at my work to automate the creation of VMs. This way new developers coming on to a project have basically zero configuration to do in order to start working.

If you're collaborating with other devs, they don't need to do any of the above to work on your Django project, but if there's a lot of configuration involved that can't be done with pip and a requirements.txt, then you might look at Vagrant to ease some of that automation.

But you are correct in your assumption that you can all just work on a local branch and push back to the repo. Everything else is just icing.

like image 153
Brandon Avatar answered Sep 28 '22 22:09

Brandon


Virtualenv is a python construct that holds a specific set of packages, separate from your system packages. The version of Python and its packages that came with your OS or that you installed separately is a "system package".

Virtualbox is totally different -- it's a VM, an entire operating system in a box.

I'm not familiar with Vagrant.

All you need is virtualenv. Create a new virtualenv for each project (they're very lightweight!) You need to do this because the whole point of virtualenv is to isolate the exact packages and versions of those packages you need for your project. Then activate the virtualenv and use pip install to install the packages you need, presumably starting with Django itself.

Once you have all the packages you need, use pip freeze > requirements.txt to create a file called requirements.txt that records all of the packages you've decided to use.

When other people collaborate on your project, they can start a virtualenv, pull your code into it, and run pip install -r requirements.txt to replicate your environment. They can even modify requirements.txt, push that back to you via your version control system, and you can run pip install -r requirements.txt yourself to modify your environment to match their changes.

This is all essential because without virtualenv, the problem of, for instance, having one project on your computer that requires Django 1.4 and one that requires Django 1.5 becomes very complicated.

Virtualenv is not an entire operating system in a box, just a python environment, so even if you are using it, you are still working on your local machine.

like image 23
Andrew Gorcester Avatar answered Sep 28 '22 20:09

Andrew Gorcester


We use virtualenv and a Ubuntu virtual machine. Here's why:

  • virtualenv allows us to have isolated Python environments on a given operating system instance
  • Using Ubuntu dekstop in a virtual machine for our Python development mimics what it will look like when deployed on the server which is also Ubuntu. This means that we understand precisely the external OS package dependencies and configuration. You don't get this easily when you use OSX or Windows for development and Linux for deployment.
like image 33
Raj Avatar answered Sep 28 '22 21:09

Raj