Skip to content
tezvyn:
Menu

Python Virtual Environments

MediumHow cards are made

A virtual environment is an isolated Python installation with its own packages, so each project gets the exact dependency versions it needs without conflicting with other projects or the system Python.

Why it exists

Python installs packages into a shared location by default, so two projects on one machine must share the same versions of every library. That breaks down quickly: one project needs an old version of a framework, another needs a new one, and a single global install cannot satisfy both. Virtual environments exist to give each project its own isolated set of dependencies.

The mental model

Think of a virtual environment as a sandbox folder that contains its own copy or link of the Python interpreter and its own package directory. When the environment is active, Python and pip look inside that sandbox instead of the global installation, so anything you install stays local to that project and disappears in scope when you deactivate.

How it works

Creating an environment, typically with the built-in venv module, makes a directory with a pointer to a Python interpreter and an empty site-packages folder. Activating it adjusts your shell so that python and pip resolve to the environment's versions, usually by prepending its path. Packages you install with pip then land in that folder only. Recording exact versions in a requirements file, often produced with pip freeze, lets anyone recreate the same environment elsewhere by installing from that file into a fresh environment. Deactivating returns you to the system Python.

When it matters

Use a virtual environment for essentially every project beyond a throwaway script, especially when collaborating, deploying, or running multiple projects with differing dependencies. It matters most for reproducibility: a teammate or a production server can rebuild the identical dependency set. It also keeps the system Python clean, avoiding the permission issues and breakage that come from modifying global packages.

A concrete example

Suppose project A needs version 1 of a data library and project B needs version 2. You create a venv in project A, activate it, and pip install the version-1 library; it lands only in A's folder. You do the same in project B with version 2. Each project runs with its required version, neither interferes with the other, and pip freeze in each produces a requirements file so a colleague can reproduce both setups exactly on a different machine without any conflict.

Interview question

What core problem do Python virtual environments primarily solve?

  • a.They make Python code run faster at execution time
  • b.They automatically write the source code for new projects
  • c.They isolate each project's dependencies so conflicting versions can coexistCorrect
  • d.They encrypt installed packages for security
Why?

Virtual environments give each project its own isolated packages, so two projects needing different versions of a library no longer conflict. They do not affect runtime speed or generate code, ruling out the other options.

Just read this? Test yourself on what you have been reading.

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.

See open roles