Skip to content
tezvyn:

Python Virtual Environments: Isolate Project Dependencies

Source: docs.python.orgEasyHow cards are made

Python Virtual Environments: Isolate Project Dependencies

A Python virtual environment is a self-contained directory with its own Python interpreter and packages, preventing dependency conflicts between projects. The biggest mistake is checking the environment folder into source control; it's disposable and meant to…

Why it exists

Python projects often depend on specific versions of external libraries. If you install these globally, two different projects might require conflicting versions of the same library, leading to 'dependency hell'. Virtual environments were created to solve this by providing isolated spaces for each project's dependencies.

The mental model

Think of a virtual environment as a clean, separate workshop for each of your Python projects. Each workshop has its own dedicated set of tools (packages) and its own copy of the main machinery (the Python interpreter). You can install, upgrade, or remove tools in one workshop without affecting any of the others or the main factory (your system's global Python).

How it works

Running python -m venv my-env creates a new directory named my-env. Inside, it sets up a pyvenv.cfg file that points to your base Python installation. It also creates a bin subdirectory (or Scripts on Windows) with a copy of the Python executable and an activate script. When you run the activate script, your shell's command path is temporarily altered to prioritize the Python and pip executables within your my-env/bin directory. Any packages you install with pip are then placed in the environment's local site-packages folder, not the system-wide one.

When to use it

Use a virtual environment for nearly every Python project you work on. It is the standard, recommended practice for managing dependencies. It ensures your project is self-contained, its dependencies are explicitly declared, and it can be reliably reproduced by other developers on other machines.

When not to use it

Virtual environments are not designed to be moved or copied between machines; you should recreate them from a requirements file instead. They are also not supported on certain platforms like mobile operating systems (Android/iOS) or WebAssembly. For simple, one-off scripts with zero external dependencies, a venv might be overkill, but it's a low-cost habit to maintain.

One canonical example

To create and use a virtual environment for a new project, you follow three steps. First, create the environment: python -m venv .venv. Second, activate it (on macOS/Linux): source .venv/bin/activate. Your command prompt will change to show you're inside the environment. Third, install your dependencies, which now go into the isolated environment: pip install requests. When you're done, simply type deactivate.

Interview question

Which problem does a Python virtual environment primarily aim to solve?

  • a.The slow execution speed of Python applications due to unoptimized package installations.
  • b.The challenge of installing Python packages on systems without administrative privileges.
  • c.The difficulty of sharing project code and its dependencies with other developers.
  • d.Conflicts arising when different projects require incompatible versions of the same Python library.Correct
Why?

The card explicitly states that virtual environments were created to solve 'dependency hell,' which arises when 'two different projects might require conflicting versions of the same library.' While virtual environments aid in reproducibility (which helps sharing), their primary purpose is to prevent these direct dependency conflicts.

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

Read the original → docs.python.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

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