Python Virtual Environments Explained

What is virtualenv?

The virtualenv tool creates an isolated Python environment (in the form of a directory) that is completely separate from the system-wide Python environment.

What this really means is that any settings, 3rd-party packages, etc. from the system-wide environment do not appear in the virtual environment, so it's almost like you have a clean Python install.

This is useful for when you want to have a clean-slate for your projects. Let's say you have boto version 2.7.0 installed in the site-packages, but the project you're just starting needs the newer 2.38.0 version. Since you can't have both versions installed site-wide, you need a Python environment that will keep the dependencies separated. This is what the virtualenv tool is for.

Why is virtualenv Useful?

Python is unlike other more enterprise-friendly languages (like Java) in that 3rd-party libraries are loaded and used throughout the whole system, instead of on a project-by-project basis. This can become a problem if two different projects require different versions of the same package.

So for each project you start, you can also create a new virtual environment to ensure that all of the installed dependencies don't affect the other projects on your computer.

As you create more and more projects, and as you deploy those projects, you'll soon realize how important it is to have strict separations between projects.

How do you use virtualenv?

Virtual environments are easy to create (and destroy), only requiring the virtualenv package, which can be installed with:

$ pip install virtualenv

To create a new virtual environment, you'll likely want to do something like this:

$ virtualenv --no-site-packages myapp

This command will create the following directory structure:

  • myapp/
    • bin/
    • include/
    • lib/

Using the --no-site-packages flag creates a virtual environment that resembles a clean Python install, and which contains no 3rd-party packages, but only the standard Python packages.

The three sub-directories listed above contain all of the Python executables, dependencies, and packages required to develop and run Python programs. It also includes some useful tools like pip and easy_install.

And finally, in order to use a particular virtual environment, activate it with:

Scotts-Computer:Projects: scott$ cd myapp/
Scotts-Computer:myapp scott$ source bin/activate
(myapp)Scotts-Computer:myapp scott$ 
Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Notice that this prefixes your command prompt with the name of the virtualenv (myapp in this case), which indicates that your current Python environment is the myapp virtual environment.

Now every time you run a Python script, the virtual environment's Python executable, settings, and packages will be used instead of the global Python executable.

To stop using the virtual environment, simply deactivate it by running:

(myapp)Scotts-Computer:myapp scott$ deactivate
Scotts-Computer:myapp scott$

Conclusion

In Python, and just about every other programming language, it is important to have full control over your environment so you know exactly what is going on with your code and how to replicate it on any machine.

Virtual environments help you do this by separating out global configurations and code from local code.

Last Updated: July 3rd, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Project

Building Your First Convolutional Neural Network With Keras

# python# artificial intelligence# machine learning# tensorflow

Most resources start with pristine datasets, start at importing and finish at validation. There's much more to know. Why was a class predicted? Where was...

David Landup
David Landup
Details
Course

Data Visualization in Python with Matplotlib and Pandas

# python# pandas# matplotlib

Data Visualization in Python with Matplotlib and Pandas is a course designed to take absolute beginners to Pandas and Matplotlib, with basic Python knowledge, and...

David Landup
David Landup
Details

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms