Working With Python Virtual Environments

Dan Bader

Dan Bader 6 Lessons 8m
basics tools

When you install Python packages globally there can be only one version of a Python library across all of your programs. This means you’ll quickly run into version conflicts.

The solution to these problems is separating your Python environments with so-called virtual environments. They allow you to separate Python dependencies by project, including selecting between different versions of the Python interpreter.

A Virtual Environment (or “virtualenv”, “venv” for short) is an isolated Python environment. Physically, it lives inside a folder containing all the packages and other dependencies, like native-code libraries and the interpreter runtime, that a Python project needs.

To demonstrate how virtual environments work as a “sandbox” I’ll give you a quick walkthrough where we’ll set up a new environment (or virtualenv, as they’re called for short) and then install a third-party package into it using the Python pip command.

About Dan Bader

Dan Bader Dan Bader

Dan Bader is the owner and editor in chief of Real Python and the main developer of the realpython.com learning platform. Dan has been writing code for more than 20 years and holds a master's degree in computer science.

» More about Dan

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Participant Comments

Alan ODannel on May 6, 2020

I’ve used the Python virtual environments, it’s been a while and I wanted a refresher. This was just what I needed.

Brandon Austin on March 30, 2020

Great and quick to the punch course. Good stuff Dan - really loving the content.

Rob Black on Aug. 13, 2019

Excellent micro course on virtual environments - exactly the size and pace that I needed. I viewed this course as a side-bar to the tutorial I’m reading now: realpython.com/flask-connexion-rest-api/. Thanks!

Abby Jones on June 27, 2019

This is awesome.

Jet on June 13, 2019

Really great to find so many good tutorials on important topics. The only thing I keep running into: it is almost always not clear what version of Python (or Django, or ....) is used. That can make a lot of difference re. commands you should use etc. It would save students so much time if this information would be given upfront. Now, I often follow a tutorial for 10 mins or so, only to find out it uses Python 2 instead of 3, or Windows instead of OS X, and then I quit. Especially for beginners this information can be very helpful!

charliem22 on May 28, 2019

Great job! I’m just staring my first ‘big’ project and needed to understand venv machinary. This tutorial was exactly what I needed. Thanks Dan!!! –charlie

Douglas Fabretti on April 10, 2019

Nicely explained, short and concise! Thank you very much for sharing this series with us Dan.

« Browse All Courses