Skip to content

Instantly share code, notes, and snippets.

@shahidhk
Created November 16, 2013 11:34
Show Gist options
  • Select an option

  • Save shahidhk/7499206 to your computer and use it in GitHub Desktop.

Select an option

Save shahidhk/7499206 to your computer and use it in GitHub Desktop.
Installation using distribute and pip
NOTE: If you don't want to touch system files or you don't have root, please see this question on stackoverflow. For everyone else please read on...
Install distribute and pip
According to the distribute website setuptools and easy_install are old and busted (the version included in Ubuntu 12.04 doesn't work with python3), and distribute and pip are the new hotness. So we will use those:
curl -O http://python-distribute.org/distribute_setup.py
sudo python distribute_setup.py
sudo easy_install pip
Optional: Turn on bash autocomplete for pip
Run
pip completion --bash >> ~/.bashrc
and run source ~/.bashrc to enable
Use pip to install virtualenv and virtualenvwrapper
The reason we are also installing virtualenvwrapper is because it offers nice and simple commands to manage your virtual environments.
sudo pip install virtualenv
sudo pip install virtualenvwrapper
Setup virtualenv
First we export the WORKON_HOME variable which contains the directory in which our virtual environments are to be stored. Let's make this ~/.virtualenvs
export WORKON_HOME=`~/.virtualenvs`
now also create this directory
mkdir $WORKON_HOME
and put this export in our ~/.bashrc file so this variable gets automatically defined
echo "export WORKON_HOME=$WORKON_HOME" >> ~/.bashrc
Setup virtualenvwrapper
To use virtualenvwrapper we need to import its functions in our ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
Also we will create an alias for mkvirtualenv so that by default it does not use the system wide site-packages, and does use distribute rather than setuptools:
echo "alias mkvirtualenv='mkvirtualenv --no-site-packages --distribute'" >> ~/.bashrc
we can also add some extra tricks like the following, which makes sure that if pip creates an extra virtual environment, it is also placed in our WORKON_HOME directory:
echo "export PIP_VIRTUALENV_BASE=$WORKON_HOME" >> ~/.bashrc
Source ~/.bashrc to load the changes
source ~/.bashrc
Test if it works
Now we create our first virtual environment
mkvirtualenv test
You will see that the environment will be set up, and your prompt now includes the name of your active environment in parentheses. Also if you now run
python -c "import sys; print sys.path"
you should see a lot of /home/user/.virtualenv/... because it now doesn't use your system site-packages.
You can deactivate your environment by running
deactivate
and if you want to work on it again, simply type
workon test
Finally, if you want to delete your environment, type
rmvirtualenv test
Enjoy!
Important note:
All virtual environments have pip automatically installed, never call pip with sudo in your virtual environment, because this will use the system wide pip.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment