Python Virtualenv Examples

Python Virtualenv Examples

Last updated:
Table of Contents

If you need to work with python you'll sooner or later bump into one of these terms so they will be defined here for future reference.

  • pip

Package installer for Python. Like gem for ruby.

  • virtualenv

A tool to help you keep multiple versions of python (and separate python packages) for each of your projects. Think of rvm and gemsets for ruby.

Create and activate virtualenv

$ virtualenv <your-env-name>
$ . <your-env-name>/bin/activate

(to deactivate it, just run deactivate from anywhere in your system

Create virtualenv with system packages

To include system packages (i.e. those you have installed with apt-get or the like) use --system-site-packages

$ virtualenv <your-env-name> --system-site-packages

Create virtualenv using Python 3 interpreter

$ virtualenv -p python3 <your-env-name>

Install package in current virtualenv

In other wrods, in the currently activated virtualenv

(my-env)$ pip install <package-name>

Install a package globally

$ sudo pip install <package-name>