› Module 11: Modules and Packages › 11.2 Packages and pip
M O D U L E

Module 11: Modules and Packages

Organizing code across multiple files, and tapping into Python's vast built-in and third-party library ecosystem.

11.1 Importing Modules

Any .py file is automatically a "module" that other files are free to import from.

  • Writing import math gives you access to everything defined inside that module through dot notation, such as math.sqrt(16).
  • If you'd rather not type the module name prefix every time, from math import sqrt imports that one specific name directly into your own file's namespace, letting you simply write sqrt(16).
  • Finally, import module as alias lets you rename a module for convenience at the point of import — import numpy as np is such a common convention in the data-science world that it's practically a second name for the library itself.
math.py (a module = any .py file) import math math.sqrt(16) mypackage/ __init__.py utils.py from mypackage import utils
Figure 11.1 — Modules are individually importable files; packages are directories of modules bundled together.

11.2 Packages and pip

A package is simply a directory containing several related modules, plus a special __init__.py file that marks the whole directory as importable as one unit — allowing large codebases to be organized hierarchically, such as mypackage.utils.helpers.

pip is Python's standard package manager, used to install third-party libraries from the Python Package Index (PyPI): for example, pip install requests.

Virtual environments (created with venv) give each individual project its own isolated set of installed packages, preventing version conflicts between different projects that happen to live on the same machine.

# Standard Virtual Environment Workflow
# 1. Create an isolated virtual environment
python -m venv .venv

# 2. Activate it:
# Windows (PowerShell):
.venv\Scripts\Activate.ps1
# Mac/Linux:
source .venv/bin/activate

# 3. Install dependencies without affecting system Python:
pip install requests pandas
MODULE COMPREHENSIVE QUIZ

Module 11 Final Quiz

Test your understanding across all 2 topics of this module with interview-grade questions, tricky edge cases, and code output predictions.

Take Module 11 Final Quiz →
Yukti Mascot