Virtualenv 和源版本控制

我最近开始了一个 Django 项目,我很快意识到,由于很多原因,viralenv 将非常有用。我设置了 viralenv 和我的项目,但是现在我想知道我应该向源代码控制添加什么文件(在我的例子中是 Mercurial)。我应该在 venv 文件夹下添加所有文件吗?我如何确保一个同事可以克隆并立即开始工作,而不必重新设置环境?

18275 次浏览

All these environment hassles are kind of common when you are doing python/django development! I went through all these problem, and I have tested some solutions! Things that I have tested:

  1. Project running local
  2. Project running in virtualenv
  3. Project running in a VM
  4. Project running in a VM, using vagrant

The best solution I found was #4! because the company that I used to work, each person in the team has a different OS, all sort of windows, mac and linux, and to install all dependencies for each environment it takes time! So we decided to try virtualenv, which is really good! but still each person has to setup his own enviroument. The problem in virtualenv is that all python sources are within the environment that u create! So I would not push those files to a source version control! Best solution was #4, because that was exactly what I needed, Vagrant uses Chef to setup your environment, so you just have to write some recipes, and let vagrant run them for u! Then u push those recipes to SCM, then when the next person get the files from SCM and reloads the VM all dependencies will be automatically install!

I have a blog post explaining more about the subject as well as I have created a Django Blank project in github so you can get that to have a start point of your project using vagrant.

http://arthurnn.com/blog/2011/11/25/easy-django-quickstart/ (link no longer active, so linked to Wayback Machine)

EDIT

Solution from Chris Pratt is a good one as well, however some libraries are not so straightforward to install in all OS, for instance, a lot people on Mac get problems when they want to install MySQLdb-python. which is a really common library, but if everyone in your team has to spend time solving this issues, is not good at all!

You generate a "requirements" file (usually requirements.txt) that you commit with your project:

pip freeze > requirements.txt

Then, each developer will set up their own virtualenv and run:

pip install -r requirements.txt