Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Python virtual environments are used to create isolated python environments to avoid dependency and version conflicts, and also indirectly takes care of permission issues. But what is the easiest way to set it up, and use it, in Ubuntu?

share|improve this question
add comment

2 Answers

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

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.

Thanks to the author of this blogpost

share|improve this answer
1  
python-pip and python-virtualenv are installable through the Ubuntu repsitory, no need for manual installation. –  Timo Jan 18 '13 at 15:18
1  
@Timo I know, but the python packages in Ubuntu are outdated. Maintaining python packages with pip is much easier. Also the pip from Ubuntu (12.04) is from setup-tools which doesn't support python3. –  Gerhard Burger Jan 18 '13 at 17:30
    
However, in Ubuntu, we recommend Ubuntu packages, for many reasons. There's nothing wrong with older versions of pip or virtualenv, they are perfectly capable. –  tumbleweed Jan 18 '13 at 20:04
    
@tumbleweed Like a already said, the pip version in ubuntu does not work with python3, doesn't seem "perfectly capable" to me. Besides, the packages you will install IN your virtualenv will not be Ubuntu packages. So if you only want Ubuntu packages you better not install virtualenv... –  Gerhard Burger Jan 18 '13 at 21:14
    
@GerhardBurger: If you create a python3 virtualenv, it'll get a pip that works in python3. If you use Ubuntu's virtualenv to create the virtualenv, everything you've done will be contained within the virtualenv. If you start sudo easy_installing stuff, it'll leave a mess all over /usr/local, that's non-trivial to clean up, without much gain. –  tumbleweed Jan 19 '13 at 13:30
show 4 more comments

It's easy, you install python-virtualenv. Then you can create a virtualenv with the virtualenv command. See their documentation for more.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.