virtualenvs info gotchas - RichardForshaw/dev-tools-blog GitHub Wiki

Virtual Environments: Info and Gotchas

VirtualEnv & VirtualEnvWrapper

VirtualEnv is a virtual environment manager for developing python projects. It allows you to create isolated python environments so you can work on multiple project without getting your packages and dependencies tangled. VirtualEnvWrapper is a set of extensions to this module which aligns the functions to your development workflow.

Essentially:

  1. Create a new environment with mkvirtualenv
  2. Make sure you are working on your env with workon \<myenv\>
  3. Install packages with pip
  4. Show the packages you are using with lssitepackages
  5. Exit your environment with deactivate

Gotchas

  1. Make sure you install both virtualenvwrapper with apt-get and pip install
  2. Make sure you put source /<location>/virtualenvwrapper.sh in your .bashrc
  3. Make sure you put /usr/lib/<pythonlocation>/dist-packages/ in your PYTHONPATH before running any virtualenv commands
  4. If you have recently upreaded python from less than version 2.7.6, make sure you do the following:
  5. Make sure you have the right pip!!. Python >2.7.5 comes with its own pip, so you will need to uninstall the 'external' pip: sudo apt-get remove python-pip
  6. Make sure you have the latest PIP running (i.e. the one from your upgrade). If not, run easy_install pip
  7. Make sure you have the latest virtualenvwrapper python libraries: pip install virtualenvwrapper or pip install -U virtualenvwrapper. Virtualenvwrapper needs the shell scripts AND the python libraries. If not when it makes the new environment it won't install you latest python version in the environment and you will get all sorts of installation headaches.

AutoEnv

Similar to VirtualEnv but for managing your command shell environment. It allows you to put a ".env" file in a directory which defines the environment specification for working in that directory and subdirectories.

Installation Gotcha: The documentation fails to mention that it installs a script to /usr/local/bin/activate.sh which you need to reference in your bashrc. If you don't do this it won't work!!!

Usage Gotcha: AutoEnv overrides the cd command, so if you rely on your own override be careful. It also means that it might not work for pushd/popd. I've not tested this yet. It also only invokes environment changes when it encounters a ".env" file, so if you cd up and out of that directory, it does not deactivate the environment!!!

⚠️ **GitHub.com Fallback** ⚠️