install:
1pip install virtualenvwrapper
We need to specify the directory that’ll be the home of all of our virtual environments. To do that we need to set an environment variable WORKON_HOME
and set it’s value to our virtualenv home directory. We also need to source the actual virtualenvwrapper shell script that’ll allow us to run virtualenvwrapper commands.
1# Set the WORKON_HOME variable for virtualenvwrapper
2export WORKON_HOME="~/.virtualenvs"
3
4# Source the virtualenvwrapper shell script to be able to run commands
5source /usr/local/bin/virtualenvwrapper.sh
All in one command:
1echo -e '\n# Set the WORKON_HOME variable for virtualenvwrapper \nexport WORKON_HOME="~/.virtualenvs"' >> ~/.bash_profile && echo -e '\n# Source the virtualenvwrapper shell script to be able to run commands \nsource /usr/local/bin/virtualenvwrapper.sh' >> ~/.bash_profile && source ~/.bash_profile
mkvirtualenv foo
Use tab
for command auto-completion (or make an alias for the command).
Creating a virtual environment also automatically activates it (contrary to virtualenv).
to deactivate environment:
deactivate
cdvirtualenv
get a list of all the virtual environments:
workon
switch environnment:
workon foobar
list packages for the environment:
lssitepackages
go to site packages directory:
cdsitepackages
Activate/switch to the virtual environment and cd to the project folder and run the command setvirtualenvproject
while you have the virtualenv activated. For example:
(foo)[~] $ cd ~/Sandbox/foo
(foo)[~/Sandbox/foo] $ setvirtualenvproject
The setvirtualenvproject
creates a .project
file in the virtualenv directory and adds the path of the project to it. To remove the project connection all you have to do is delete this file.
Hooks basically let you run commands at certain intervals/places in the setup.
For example, postmkvirtualenv
lets you run commands after an env is created, postactivate
lets you automatically run commands right after an env is activated. predeactivate
lets you run a command before an env is deactivated. These hook files are located in $VIRTUAL_ENV/bin