OOP & ModulesExtra· 30 min read

Modules & pip

Reuse code across files and install free packages from the world’s biggest library.

What you will learn

  • Import modules
  • Use the standard library
  • Install packages with pip

Import code

A module is a file of ready-made code you can reuse. Python ships with a huge standard library of them. You bring a module into your program with import, then use its tools through the dot.

Import built-in modules
import math
print(math.sqrt(16))    # 4.0

from datetime import date
print(date.today())

The first import math loads the whole maths module, so math.sqrt(16) asks it for the square root of 16, which is 4.0. The second style, from datetime import date, pulls just one tool (date) out of the datetime module so you can use it by name — date.today() then returns today’s date. Use import x when you want the whole module, and from x import y when you only need one piece.

Note: Output: 4.0 2026-06-07 (The date shown is whatever day you run it.)

Install packages with pip

Beyond the built-in modules, the world has written millions more. Python’s package manager pip installs these free libraries (like Django!) from an online store called PyPI.

Terminal: install a package with pip
pip install requests
# then in code:
# import requests

You run pip install requests in the terminal, not inside your Python file. It downloads the requests package and saves it on your computer. After that, any of your programs can pull it in with import requests — exactly like the built-in modules above.

Note: Output: Successfully installed requests-2.32.3 (Your version number may differ.)

Tip: It is good practice to use a virtual environment (python -m venv venv) so each project has its own packages. You will use this for Django.

Q. What tool installs Python packages?

Answer: pip is Python’s package installer (pip install <name>).

✍️ Practice

  1. Use the math module to find a square root.
  2. Install a package with pip.

🏠 Homework

  1. Create a virtual environment and install a package inside it.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →