Python Master Course V2 — 065. pip and Package Installation
SMARTYFY ACADEMY · PYTHON MASTER COURSE V2 · MODULE 07 — MODULES AND PACKAGES · LESSON 065 / 200
pip and Package Installation
Estimated study: 60–90 minutes. Prerequisites: functions, collections, control flow and the preceding lesson. Level: intermediate Python.
Video lesson
Video not yet available. Reserved for a verified video URL/player; written learning materials are available now.
Learning outcomes
Install dependencies with version awareness. After completing this lesson, you should be able to explain the underlying mechanism, run the example, identify a boundary case, and apply the concept in a practical program.
Understand pip and Package Installation
Install into the interpreter that will run the program
pip installs Python distributions into an environment. The distribution name used with pip may differ from the import name used in source code, so check the project’s official documentation rather than guessing. Importing a package does not install it. Conversely, successful installation into one Python environment does not make the package available to every interpreter on the computer.
Use python -m pip, or an explicit interpreter path followed by -m pip, to tie installation to the intended Python. A bare pip command can resolve to a different installation through the shell’s PATH. The following workflow uses a disposable virtual environment so system-managed Python packages remain untouched. Lesson 066 explains how that isolation works. Choose the commands for your operating system; these are terminal commands, not lines to paste into the Python prompt.
python3 -m venv .venv
.venv/bin/python -m pip --version
.venv/bin/python -m pip install packaging
.venv/bin/python -m pip show packaging
.venv/bin/python -m pip check
py -3 -m venv .venv
.venv\Scripts\python.exe -m pip --version
.venv\Scripts\python.exe -m pip install packaging
.venv\Scripts\python.exe -m pip show packaging
.venv\Scripts\python.exe -m pip check
The installer output and installed version depend on the interpreter, platform, package index and date; no fixed version is promised here. Read the location printed by pip –version and pip show to verify that installation targets .venv. pip check inspects declared dependency compatibility; it does not prove that the package is safe or that your application behaves correctly. Follow installation with a small import and behavior check.
from packaging.version import Version
print(Version("2.10") > Version("2.9"))
.venv/bin/python check_install.py
.venv\Scripts\python.exe check_install.py
True
Diagnose the right failure
A ModuleNotFoundError after installation often means the editor or terminal uses a different interpreter. Print sys.executable from the failing program and compare it with the interpreter used for pip. An installation failure can instead reflect an unsupported Python version, unavailable release, network problem, missing build prerequisites or permission issue. Read the first actionable error rather than repeatedly running unrelated commands. Do not disable certificate verification or use an administrator install as a routine workaround.
If pip is missing in a standard Python environment, consult the platform’s Python installation instructions; some operating-system distributions split venv or pip into separately managed components. Recreate a practice environment with the supported tooling instead of mixing system and project package management. An “externally managed environment” message is a reason to use an isolated project environment, not to override the protection blindly.
Record what the project needs
Install only dependencies needed for the exercise, obtain them from the intended registry or official source, and verify spelling to avoid similarly named projects. A version range states compatibility expectations; an exact pin states one selected release. Lesson 067 turns the successful environment into a reviewed dependency record. Keep installation commands out of normal program import paths: a library should not silently install packages when another program imports it. See the pip user guide for interpreter-bound installation and inspection commands.
Optional metadata-inspection warmup
The original metadata check assumes that this environment has pip distribution metadata. A minimal runtime may raise PackageNotFoundError. Use the isolated installation workflow above as the main practical example.
Requires Python 3.10+. Save the example as lesson65.py and run python lesson65.py (or python3). If it writes a file, run it in a disposable practice directory.
import importlib.metadata as md
print(md.version('pip').split('.')[0].isdigit())
Expected output when pip metadata has a numeric major version:
True
Step-by-step walkthrough
Read each statement in execution order. Identify imported modules, assigned variables, functions and the result passed to print(). Compare the output with the expected output and then change one input. Explain why the behavior changed. When an exception occurs, read the final traceback line and inspect the call that produced it.
Extension example
Refactor the central calculation or operation into a named function with a docstring and type hints. Add a caller that invokes it twice with different inputs. Test both results with assertions; for file operations, also test that the expected file exists and clean up practice files afterward.
Guided project — pip and Package Installation application
Document reproducible installation steps for a small package.
- Write a short requirements document specifying input data, output data and expected errors.
- Implement one focused function or module for the core behavior.
- Write a separate program entry point to collect or construct example input.
- Add validation and informative errors where appropriate.
- Test a normal case, a boundary case and an invalid case.
- Write a README showing how to run and test the program.
Acceptance criteria: clear behavior, no unexplained dependencies, reproducible execution and a documented result. The project is a guided assignment; its implementation must be completed and tested by the learner.
Common mistakes, debugging and best practices
- Importing a local file with the same name as a standard-library module, causing shadowing.
- Using broad exception handlers that conceal the real defect.
- Assuming a path or dependency exists on every computer.
- Mixing input/output and reusable business logic into one difficult-to-test function.
- Testing only the happy path rather than edge cases and failures.
Debugging procedure: reproduce the failure with the smallest input; read the traceback; verify assumptions and data types; change one thing; rerun the same test; add a regression test after the fix.
Practice and explained answers
Try each question before opening its answer. Predict code results on paper, then compare them with the explanation.
1. Why prefer an explicit interpreter with -m pip?
Need a hint?
Compare the interpreter running pip with the interpreter running the program.
Reveal answer and explanation
Answer and reasoning:
It runs pip in that interpreter’s environment. This reduces the common mismatch where pip installs into one Python while the program runs in another.
2. Does import packaging download packaging?
Reveal answer and explanation
Answer and reasoning:
No. Import searches the selected interpreter’s import path for available code. Install the distribution separately, then run the program with the same interpreter.
3. What should you inspect when installation succeeds but import fails?
Reveal answer and explanation
Answer and reasoning:
Check the failing program’s sys.executable, the interpreter used for installation and the package location from python -m pip show packaging. Also check for local files that shadow the package.
4. Is a successful pip check sufficient application testing?
Reveal answer and explanation
Answer and reasoning:
No. It checks declared dependency relationships. It does not execute your business behavior, validate network responses or establish that a dependency has no vulnerabilities. Add a small import/behavior test and the project’s normal tests.
5. Why does the version example print True rather than compare the strings directly?
Reveal answer and explanation
Answer and reasoning:
Version objects understand numeric release components, so 2.10 is newer than 2.9. Ordinary lexical string comparison compares characters and is unsuitable for version ordering. The example verifies a real installed capability without making a network request.
Mastery checkpoint and summary
Demonstrate the concept without copying, explain one failure mode, complete the guided project and write three independent tests. These are suggested mastery checks, not account-synchronized completion records.
Key takeaway: Install dependencies with version awareness. Apply it through clear contracts, thoughtful error handling, and repeatable testing.
Finish the exercises and mastery checkpoint before marking this lesson complete. Completion is stored on this browser.