Skip to content

Prepare a virtual environment

Nilesh edited this page Oct 1, 2011 · 3 revisions
Make sure to install Xcode on mac  
sudo easy_install pip    Mac Lion already has easy_install. sudo apt-get install python-setuptools for linux   
sudo pip install virtualenv virtualenvwrapper  
mkdir ~/domains  
  • append this to ~/.bash_profile in mac or ~/.bashrc in linux
export WORKON_HOME=$HOME/domains  
source /usr/local/bin/virtualenvwrapper.sh  
export PIP_VIRTUALENV_BASE=$WORKON_HOME  
  • exit out of that shell, start a new shell/terminal

Now you can create any number of python virtual environments. Lets create a new virtual environment we call workhere

macbook:~ nilesh$ mkvirtualenv workhere
New python executable in test/bin/python
Installing setuptools............done.
Installing pip...............done.
virtualenvwrapper.user_scripts creating /Users/nilesh/domains/workhere/bin/predeactivate
virtualenvwrapper.user_scripts creating /Users/nilesh/domains/workhere/bin/postdeactivate
virtualenvwrapper.user_scripts creating /Users/nilesh/domains/workhere/bin/preactivate
virtualenvwrapper.user_scripts creating /Users/nilesh/domains/workhere/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/nilesh/domains/workhere/bin/get_env_details
(workhere)macbook:~ nilesh$ 
(workhere)nilesh@ubuntu:~$ cd ~/domains/workhere
...
....you work here...
...
and deactivate when you are done.
(workhere)macbook:workhere nilesh$ deactivate
macbook:workhere nilesh$
  • To work with virtualenv again, simply type below
workon workhere
cd ~/domains/workhere

###Wrappers

mkvirtualenv (create a new virtualenv)
rmvirtualenv (remove an existing virtualenv)
workon (change the current virtualenv)
add2virtualenv (add external packages in a .pth file to current virtualenv)
cdsitepackages (cd into the site-packages directory of current virtualenv)
cdvirtualenv (cd into the root of the current virtualenv)
deactivate (deactivate virtualenv, which calls several hooks)

###Hooks

One of the coolest things about virtualenvwrapper is the ability to provide hooks when an event occurs. Hook files can be placed in ENV/bin/ and are simply plain-text files with shell commands. virtualenvwrapper provides the following hooks:

postmkvirtualenv
prermvirtualenv
postrmvirtualenv
postactivate
predeactivate
postdeactivate
Clone this wiki locally