Why use virtualenv?
- it lets you use a newer version of python () instead of using the system default (2.7.5 on Mac), without effecting the default. It is recommeneded that you start using Python 3 but at the same time it is not really all that feasible yet for several project. For example, Django still is not Python 3 ready. Virtualenv lets you work in different version of python at the same time.
- it is a boxed environment. You can install different packages in different environments. They won’t interfare with each other.
sudo pip install virtualenv
By default a new virtual environment is started with no packages installed. If you want to start with access to all system packages, start virtualenv with the
starting a virtual environment:
cd to the folder you want to start a virtual environment in.
where foo is the name of your environment. This will create a virtualenv, but not activate it.
In order to activate, source the activate script:
To see the packages installed inside of our virtual environment:
to stop a virtualenv:
Specifying which Python version to use:
-p flag is for the python interpreator to use. The default is the interpretor that python was installed with (/usr/bin/python)
start virtualenv with Python 3:
virtualenv -p python3 foobar
You odn’t really have to give it the full path, you can just say python3.
To check python version:
There are two ways to do it. One is to pass attributes to export command and the other is to config the virtualenv.ini file.
Take the attribute name, replaces all the dashes (-) with underscroes (_) and then prefixing that with
For example, we are going to setup
distribute to be the default package management library that we are installing with our virtualenv.
Now if you create a virtualenv it’ll install distribute by default. If you want this to be permanent, you can save the export statement to your ~/.bashrc or ~/.bash_profile or any of your startup scripts that you have.
The config file is supposed to in the
~/.virtualenv folder which may not exist. To create and edit a
sudo mkdir ~/.virtualenv && touch ~/.virtualenv/virtualenv.ini && nano ~/.virtualenv/virtualenv.ini
[virtualenv] python = python3 [/virtualenv]