In general what are the best practices for using virtualenv with version control?
In general what are the best practices for using virtualenv with version control?
Generally you should strongly avoid putting generated artefacts into version control. This leads to complete pain if ever you find yourself trying to diff or merge when they inevitably change. The problem is that you end up with conflicts which are completely unnecessary - you should always be able to just regenerate the virtualenv at any time.
This is especially true for non-relocatable artefacts (as others have mentioned) such as virtualenvs or compiled binaries.
Another thing is that these generated artefacts can be costly in terms of space consumed in the repository - maybe not so much for a virtualenv with one package in it, but for binaries or larger virtualenvs, these things can become quite large. In addition they're often not so friendly for git's delta compression which is better suited for textual data. You can end up unnecessarily increasing the size of your repository significantly, which is another thing best avoided.
I keep my virtualenvs in ~/.virtualenvs/, away from the project.
If your project is a package itself (i.e. it has a setup.py file), then use that file to specify dependencies. On a new machine I check out a copy, create a virtual env and activate it. Then in the local copy I run "pip install -e .". This installs all the requirements from setup.py in the virtualenv, and links the local copy of my project to it as well. Now your package is available in the virtual env, but fully editable.
If your python project is not a package, you can install its dependencies in a virtual env with pip. Then run "pip freeze" to generate a list of all installed packages. Save that to a text file in your repository, e.g. ``requirements.txt``. On a different machine, or a fresh venv, you can then do "pip install -r requirements.txt" to set everything up in one go.
Document it in setup.py:
if sys.version_info < (2, 6, 0):
sys.stderr.write("Foo requires Python 2.6 or newer.\n")
sys.exit(1)
You're using setup.py, right? ;) https://devcenter.heroku.com/articles/python-runtimes
I think this works well as a convention even if you're not deploying to Heroku. I also like the suggestion to put a guard in setup.py that checks sys.version_info.Just add the env directory to your .gitignore
Also, IIRC the environment will contain a symlimk to the python executable and companion files. That symlink will change depending on the environment.