Python Tutorial 0/52 lessons ~6 min read Lesson 31
Virtual Environments
Virtual environments isolate per-project dependencies — non-negotiable for any real Python work.
Course progress0%
Focus
9 guided sections
Practice signal
Examples included
Career prep
Interview Q&A included
Introduction
Virtual environments isolate per-project dependencies — non-negotiable for any real Python work.
Understanding the topic
Core concepts to understand:
python -m venv .venvcreates one.source .venv/bin/activateuses it.uvandpoetryare modern wrappers.condafor data science with non-Python deps.
Syntax reference
Visual flow / code:
bash
# Built-in venvpython -m venv .venvsource .venv/bin/activatepip install requests fastapi# uv (super-fast, modern)uv venvuv pip install fastapi# Poetrypoetry initpoetry add fastapipoetry shell
Execution workflow
1Virtual Environments Workflow
1 / 4python -m venv .venv creates one
python -m venv .venv creates one.
Real-world use
uv (from Astral, makers of Ruff) is 10-100× faster than pip and is rapidly becoming the new standard.
Best practices
- One venv per project.
- Commit
requirements.txtorpoetry.lock. - Try uv for speed.
Common mistakes
- Installing globally with sudo pip — corrupts system Python.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionvenv vs conda?+
Answer
venv is in the standard library and creates a lightweight environment using the Python interpreter you already have, with packages from PyPI via pip. Conda is a separate package manager that can install Python itself and non-Python binaries such as C libraries and CUDA toolkits. Use venv for pure-Python or wheel-based projects, and conda when you need those native dependencies managed together.Follow-up
Can you mix pip and conda in the same environment?
2BeginnerQuestionWhat does pip install -e do?+
Answer
It performs an editable (development) install: the package is registered in the active environment, but its imports resolve to your working directory rather than a copied snapshot. Dependencies from the project metadata are still installed normally. Source changes are visible immediately, which suits active development.
Follow-up
How is it different from adding the folder to
PYTHONPATH?3BeginnerQuestionWhy isolate dependencies?+
Answer
Different projects often need different versions of the same library, and one global site-packages can only hold one. Isolation prevents version conflicts, avoids breaking system tools that rely on the system Python, and makes a project's dependency set reproducible on other machines and in CI. It also keeps a clean record of what the project actually needs.
Follow-up
What breaks if you install packages into the system Python?
Summary
In summary: Always activate a venv. uv is the new fast standard.
Ready to mark this lesson complete?Track your journey across the entire course.