Enhancements to virtualenv
- Add a line like export WORKON_HOME=$HOME/.virtualenvs to your .bashrc.
- Add a line like source /path/to/this/file/virtualenvwrapper_bashrc to your .bashrc.
- Run: source ~/.bashrc
- Run: workon
- A list of environments, empty, is printed.
- Run: mkvirtualenv temp
- Run: workon
- A new environment, temp is created and activated.
- This time, the temp environment is included.
Sometimes it is desirable to share installed packages that are not in the system site-pacakges directory and which you do not want to install in each virtualenv. In this case, you could symlink the source into the environment site-packages directory, but it is also easy to add extra directories to the PYTHONPATH by including them in a .pth file inside site-packages using add2virtualenv.
- Check out the source for a big project, such as Django.
- Run: add2virtualenv path_to_source.
- Run: add2virtualenv.
- A usage message and list of current “extra” paths is printed.
virtualenvwrapper adds two separate hook scripts you can use to change your settings when moving between environments. They are sourced by workon at the appropriate trigger time, allowing them to modify your shell environment.
Both scripts are bash shell scripts and need to be saved in $VIRTUAL_ENV/bin/.
The postactivate script is run after the new environment is enabled. $VIRTUAL_ENV refers to the new environment at the time the script runs.
This example script for the PyMOTW environment changes the current working directory and the PATH variable to refer to the source tree containing the PyMOTW source.
pymotw_root=/Users/dhellmann/Documents/PyMOTW cd $pymotw_root PATH=$pymotw_root/bin:$PATH
The predeactivate script is run before the current environment is deactivated, and can be used to disable or clear settings in your environment. $VIRTUAL_ENV refers to the old environment at the time the script runs.
The function add2virtualenv adds the specified directories to the Python path for the active virtualenv. The directory names passed as argument are added to a path file named virtualenv_path_extensions.pth inside the virtualenv’s site-packages directory. If this file does not exist, it will be created first.
For more details, refer to the column I wrote for the May 2008 issue of Python Magazine: virtualenvwrapper | And Now For Something Completely Different.
- Add completion support for zsh, courtesy of Ted Leung.
- Fix some issues with spaces in directory or env names. They still don’t really work with virtualenv, though.
- Added documentation for the postactivate and predeactivate scripts.
- Includes a new .pth management function based on work contributed by James Bennett and Jannis Leidel.
- Includes a fix for a nasty bug in rmvirtualenv identified by John Shimek.