Python Tutorial 0/52 lessons ~6 min read Lesson 32

    pip & Dependency Management

    pip & Dependency Management in OOP & Modules: core concepts, worked examples, and practical guidance in one focused lesson.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    pip installs from PyPI. Modern projects pin deps in pyproject.toml or use a lockfile for reproducibility.

    Understanding the topic

    Core concepts to understand:

    • pip install pkg from PyPI.
    • pip freeze > requirements.txt.
    • pip install -r requirements.txt.
    • Lockfile = exact versions + transitive deps.

    Syntax reference

    Visual flow / code:

    bash
    # Install
    pip install requests==2.31.0
    pip install -r requirements.txt
    # Freeze current env
    pip freeze > requirements.txt
    # Compile a lockfile (pip-tools)
    pip-compile pyproject.toml
    # uv: super-fast install
    uv pip install -r requirements.txt

    Execution workflow

    1pip & Dependency Management Workflow
    1 / 4

    pip install pkg from PyPI

    pip install pkg from PyPI.

    Real-world use

    Use lockfiles (poetry.lock, uv.lock, requirements.lock) for reproducible builds. pip freeze alone is fragile.

    Best practices

    • Pin major versions in deps.
    • Use lockfile for prod.
    • Audit with pip-audit.

    Common mistakes

    • Installing unpinned packages — breaks at the worst time.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionpip vs pipx?+

    Answer

    pip installs packages into the current environment, usually to be imported by your code. pipx installs Python command-line applications, each into its own isolated virtual environment, and exposes only their executables on your PATH. Use pip for libraries and project dependencies, and pipx for tools like black or httpie.

    Follow-up

    How does pipx run differ from pipx install?
    2IntermediateQuestionrequirements.txt vs lockfile?+

    Answer

    A loose requirements.txt usually lists direct dependencies with version ranges, so installs can differ over time. A lockfile records the exact resolved versions of every package, including transitive ones, usually with hashes, so installs are reproducible. A requirements.txt can serve as a lock if it is generated by a tool such as pip-compile and pins everything.

    Follow-up

    How do hashes in a lockfile improve security?
    3BeginnerQuestionWhat's a transitive dependency?+

    Answer

    A transitive dependency is a package your project needs only because one of your direct dependencies requires it. For example, if you depend on requests, then urllib3 comes along transitively. They matter because they can introduce version conflicts and security issues you did not choose directly, which is why locking them is useful.

    Follow-up

    How can you list the dependency tree of an environment?

    Summary

    In summary: Pin + lock = reproducible. Modern tools (uv/poetry) are faster + safer.

    Ready to mark this lesson complete?Track your journey across the entire course.