LEARN PYTHON | Packages, Pip And Dependency Management

Lesson objective

Install packages into the chosen interpreter, distinguish direct and transitive dependencies and document a reproducible-enough project setup without confusing freeze with a universal lock.

Learn

07.2 | PACKAGES, PIP AND DEPENDENCY MANAGEMENT

01 | WHY BORROW A LIBRARY?

You could write every HTTP detail yourself, but a maintained library can provide a clearer interface and reduce repeated work. A library is reusable code. A Python import package groups importable modules; a distribution package is the installable project managed by tools such as pip.

Some modules, including sqlite3, json and pathlib, come with Python’s standard library. Others, such as requests, need installation. Do not run pip install sqlite3 to use the standard module.

An installation name and an import name are not guaranteed to match. For example, the distribution Pillow supplies the PIL import package. Check the publisher’s documentation instead of guessing a similarly named package.

02 | PUT THE PACKAGE IN THE RIGHT PLACE

Activate your project’s .venv from 07.1. python -m pip runs pip using that exact interpreter, reducing confusion when several Python installations exist. pip downloads and installs distributions and their declared dependencies.

Install requests for the API lesson. The range below is this course’s example compatibility constraint, not a claim that every version within it has been tested with every system. You will test your selected environment and record its installed versions.

A successful installation in one environment does not install the package in every editor interpreter. Check both sys.executable and python -m pip --version if import requests fails.

TERMINAL | ACTIVATED PROJECT ENVIRONMENT

python -m pip --version
python -m pip install "requests>=2.32,<3"
python -m pip show requests

PYTHON | VERIFY THE IMPORT

import requests
print(requests.__version__)

03 | DIRECT AND TRANSITIVE DEPENDENCIES

Your code imports requests directly, so requests is a direct dependency. Requests itself depends on other distributions, which your project receives transitively. You need to understand this chain even if you never import those supporting packages yourself.

pip resolves declared constraints and installs a compatible selection when one is available. A dependency conflict occurs when requirements cannot be satisfied together. Repeatedly forcing arbitrary versions can conceal the cause rather than solve it.

python -m pip check reports incompatible or missing declared installed dependencies. It does not prove your application works, your data is correct or every package is trustworthy.

TERMINAL | INSPECT AND CHECK

python -m pip list
python -m pip check

04 | RECORD THE PROJECT RECIPE

A requirements file is a text file containing installation requirements. It is not Python code. In the simple example, requirements.txt contains one direct dependency constraint. Install it with -r.

A version range allows updates within a policy. An exact == requirement selects one distribution version. Pinning improves repeatability, but a version number alone is not a complete record of Python, operating system, platform-specific wheels or external services.

Keep a small direct-requirements file when you want to explain what the application deliberately uses. For a concrete tested snapshot, also record all installed versions from a clean project environment.

REQUIREMENTS.TXT | TEXT FILE

requests>=2.32,<3

TERMINAL | INSTALL THE FILE

python -m pip install -r requirements.txt

05 | FREEZE A CLEAN ENVIRONMENT

pip freeze writes installed distributions in requirements-style form. Redirecting with > writes the command’s output into a file, replacing that file’s existing contents. The shell performs the redirection.

Freeze inside a clean project environment so unrelated packages do not become accidental requirements. Review the file before sharing it. The snapshot includes transitive distributions; it does not distinguish your design choices from the packages they require.

freeze is a snapshot, not a universal dependency lock or a guarantee of byte-for-byte reproducibility across machines. Document your Python version and test a fresh installation. More advanced workflows can add lock formats and artifact hashes.

TERMINAL | CREATE A SNAPSHOT

python -m pip freeze > requirements-frozen.txt

06 | REBUILD BEFORE YOU CALL IT READY

Create a separate fresh environment, install the recorded requirements and run your tests. This checks whether the recipe depends on an unrecorded global installation. Use the explicit interpreter path to avoid confusing this environment with the original one.

The example uses a different .venv-check folder. Substitute its platform-appropriate interpreter path. A failed import in the clean environment tells you your handover is incomplete even if the original environment still works.

The database task planner in 07.6 uses only the standard library. Its handover needs a Python version and run commands, but no third-party installation. The API lab separately needs requests.

TERMINAL | WINDOWS POWERSHELL

py -m venv .venv-check
.venv-check\Scripts\python.exe -m pip install -r requirements-frozen.txt
.venv-check\Scripts\python.exe -m pip check

TERMINAL | MACOS OR LINUX

python3 -m venv .venv-check
./.venv-check/bin/python -m pip install -r requirements-frozen.txt
./.venv-check/bin/python -m pip check

07 | DISTINGUISH INSTALL NAMES FROM FILE NAMES

Naming your own file requests.py can shadow the real requests package because the project location participates in Python’s import search. A strange missing-attribute error may then refer to your own file.

Rename a conflicting project file, restart the interpreter and inspect requests.__file__ to see which code was loaded. Do not confuse this module path with sys.executable, which identifies the interpreter.

Also check the exact package spelling and project source. Installing a lookalike name is not a valid fix for an import-path mistake.

import json
print("JSON module:", json.__name__)
print("Decoded value:", json.loads("[1, 2, 3]"))

EXPECTED OUTPUT

JSON module: json
Decoded value: [1, 2, 3]

08 | EXPLORE A DEPENDENCY RECORD

Choose what this fixed model records. It compares a direct constraint, a frozen environment snapshot and a dependency check. No installation occurs in your browser.

09 | UPDATE DELIBERATELY

Package installation may execute code, particularly during builds. Use the intended project and its documented source, and avoid unknown copy-and-paste install commands. A virtual environment protects package organisation; it does not make untrusted code harmless.

To update within a chosen policy, update the requirement, install in an isolated test environment and rerun application tests. Record what changed. Do not assume a successful installer means the behaviour is unchanged.

Keep credentials and tokens outside requirements files. A private index URL can contain sensitive credentials; do not commit such a URL or copy it into a public setup guide.

10 | WRITE A PARTNER-FRIENDLY HANDOVER

State the supported Python baseline, environment creation command, installation file, entry point and test command. Explain whether network access is needed for setup or normal operation.

For an API project, name the documented endpoint and what happens if it is unavailable. For the SQLite planner, say that its data file is created locally and preserved between runs. These are different dependencies.

Try it: give a partner only your source and instructions. If they require your entire .venv folder, improve the recipe before calling the project transferable.

PUT YOUR SKILLS TO WORK

Use the Section 7 challenges to practise these ideas independently. Work in your Python editor, predict results and keep a short test record.

Section 7 challenges Python course navigation

MATCH THE PYTHON TERM TO ITS MEANING

Terminology

Terminology

Library

Reusable code offering useful operations.

Import package

A grouping of importable Python modules.

Distribution

An installable project managed by package tools.

Standard library

Modules supplied with Python.

pip

A tool for installing Python distributions.

Direct dependency

A distribution deliberately required by the application itself.

Transitive dependency

A dependency required by another dependency.

Constraint

A rule limiting allowed package versions.

Version range

A requirement allowing versions within stated bounds.

Pin

A requirement selecting one exact distribution version.

Requirements file

A text list of installation requirements.

Resolver

The part of an installer choosing compatible dependency versions.

Dependency conflict

Requirements that cannot be satisfied together.

Freeze

An installed-distribution snapshot in requirements-style form.

Lock

A more explicit dependency selection record whose exact guarantees depend on its format and tooling.

Shadowing

A local name or file masking another intended name or module.

Redirection

Sending shell command output into a file.

Clean environment

An isolated environment containing the project’s intentional setup.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. Which command targets pip through the selected interpreter?
2. Which modules need no separate package install here?
3. A library required by requests is what kind of dependency for your application?
4. What does == in a requirement specify?
5. What is a freeze file?
6. What does pip check establish?
7. Why avoid a local requests.py filename?
8. What should a handover include?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Explain library.

2. Explain import package.

3. Explain distribution.

4. Explain standard library.

5. Explain pip.

6. Explain direct dependency.

7. Describe a useful test for this lesson.

8. Evaluate one limitation of the demonstrated approach.

CODE IT | YOUR INDEPENDENT CHALLENGE

Record direct requirements and a clean tested snapshot. Rebuild the environment from the recipe.

Work in your Python editor. Keep a working version, explain one design choice and record at least two tests. Use the Learn tab for worked examples before comparing your result.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for Packages, Pip And Dependency Management is coming soon. Complete the matching task and written questions in your exercise book.