Python in Virtual Environment

My first actual and hopefully useful post will be dedicated to a question I hear often: Can you run different isolated instances of Python on the same machine?

The answer to this question is relatively simple: of course, use virtual environment. A lot of people are familiar with the concept of virtualization. They usually heard about software like VMWare, VirtualBox, Parallels. These software packages allow you to run a completely new and independent instance of an operating system (guest) within your current operating system (host). So, say you would like to run Windows 7 from within Linux Ubuntu – you would go install VirtualBox, create a “virtual disk drive” for your new operating system and use CD or USB stick to proceed with normal Windows installation as you were installing it on your computer directly.

Having a guest operating system running within a host operating system is a concept called virtualization. Virtualization software basically virtualizes the hardware of your machine and allows the guest operating system to access that virtualized hardware the same way it would access the actual hardware. The concept of virtualization is very powerful and used across many different areas of computing.

Back to the original question. Python interpreter comes installed with pretty much any Linux and OSX distribution. All the libraries that are installed system wide can be used anywhere on the system. But what happens if you need a clean copy of your Python interpreter without all those libraries? You may need to setup a Django web application and later deploy it on a live server. You sure won’t deploy the whole Python stack you have on your system.

Virtual Environments come to the rescue. Similarly to the OS level virtualization, virtual environments allow you to create a new and independent environment with its set of programs and libraries from within the host environment. One of the most popular Virtual Environment software used for Python virtualization is a tool called virtualenv.

How to install virtualenv?

Well, as we established, Python itself comes with pretty much any Linux or OSX distribution, so the best way to install virtualenv, or any other package/library for that matter is using a tool called: pip. Pip is basically a package manager for Python packages. You can read more about it here, and read how to install it here.

After you installed pip, it’s really easy to install pretty much anything Python related on you machine. To install virtualenv, type:

pip install virtualenv

How to create and activate a new virtual environment?

Now that you have virtualenv installed, it’s also very easy to create a new clean virtual environment that will have a separate copy of Python interpreter and only the set of libraries you want it to have, totally isolated from the rest of the system.
To create a new virtual environment type:

mkdir venv
virtualenv venv

After your environment is ready, you can activate it and actually start using it. To “enter” or activate the new environment type:

source venv/bin/activate

You’ll notice (venv) in your terminal command line. That means that now you’re in a virtual environment and that all the commands you run here will only be executed in this particular environment. Virtualenv will install Python and pip for you, so you may immediately start installing anything you like. Just type: pip install <any_package> and it’ll be installed only for this particular environment.

How to exit the virtual environment?

So, you finished your web application and now you want to return to your host environment. Easy as it sounds, just type:

deactivate

You’ll notice that (venv) disappeared from your command line. That means you’re back in reality 🙂

The good thing about virtual environments is that you can virtually (no pun intended) create as many of them as you like. Say, one can be for Django app, second could be for that Machine Learning project you’ve been doing, third could be for a Python GUI app, and so on. The best of all, each of them with separate set of libs and packages without interfering with the others.

Extra tip: Wanna install a predefined set of libraries all at once using pip? Just create a file named: “requirements.txt”, list all the packages there and run:

pip install -r requirements.txt

Pip will go through all of them and install them for you. So you could just define requirements file for each of your smaller projects and install them in an instant.

If you have any questions, feel free to leave comments and I’ll try to answer them asap.

Cheers,
Manca