How to use virtualenv with Python? [closed]

  1. You install it in that order, see instructions below (there alternatives though). You can install virtualenv with APT before pip, but since you install pip, you don’t need to.
  2. I don’t really now, but I found this other thread that may help you: How to use pip3 with python 3.4?
  3. It is strongly recommended, dependencies may not be the same and it will help you keep things clean ( regarding version control for instance)

Now, how to install
Python
For Ubuntu 14.04 you will have python2.7 and python3 already installed, with “python” being an alias to python2.7 by default.

Pip you can install with :

sudo apt-get install python-pip python3-pip

I don’t know how pip for py2 and pip for py3 coexist but they are available as separated packages.

VirtualEnv
You can use pip to install virtualenv:

pip install virtualenv

Here I’m using pip for python2


Once a have all set I do the following:

mkdir -p project_name/source
cd project_name
virtualenv env

I usually keep source and env names constant in every project because I have some hooks around but I recommend you to replace the names, specially “env” because it’s the key to know in which VirtualEnv you are working, since you will get something like this:

(env)yser@machine:/home/user/cool_projects/project_name$

I’ve also keep env out of source to simplify things with version control (no need for marking it for ignoring) but that is just me.

To activate virtualenv:

cd project_name
source env/bin/activate

Now you can pip install inside the VirtualEnv.
To change projects exit your current VirtualEnv with:

deactivate

Hope it helps!

Leave a Comment