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
    Foundation builder

    Introduction

    Virtual environments isolate per-project dependencies — non-negotiable for any real Python work.

    Understanding the topic

    Core concepts to understand:

    • python -m venv .venv creates one.
    • source .venv/bin/activate uses it.
    • uv and poetry are modern wrappers.
    • conda for data science with non-Python deps.

    Syntax reference

    Visual flow / code:

    bash
    # Built-in venv
    python -m venv .venv
    source .venv/bin/activate
    pip install requests fastapi
    # uv (super-fast, modern)
    uv venv
    uv pip install fastapi
    # Poetry
    poetry init
    poetry add fastapi
    poetry shell

    Execution workflow

    1Virtual Environments Workflow
    1 / 4

    Step 1

    python -m venv .venv creates one.

    Apply this step while implementing virtual environments in real code.

    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.txt or poetry.lock.
    • Try uv for speed.

    Common mistakes

    • Installing globally with sudo pip — corrupts system Python.

    Hands-on exercise

    Interview preparation — practice these questions:

    • venv vs conda?
    • What does pip install -e do?
    • Why isolate dependencies?

    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.