Lesson 15 of 25

Modules & Imports

Importing Modules

A module is a Python file containing functions, classes, and variables that you can reuse across projects. Python has a rich standard library of built-in modules, and thousands more available through pip.

There are several ways to import modules: you can import the entire module, import specific items, or give a module an alias for convenience.

Example
# Import the entire module
import math
print(math.sqrt(16))    # 4.0
print(math.pi)          # 3.141592653589793
print(math.ceil(4.3))   # 5

# Import specific items
from math import sqrt, pi
print(sqrt(25))  # 5.0
print(pi)        # 3.141592653589793

# Import with an alias
import datetime as dt
now = dt.datetime.now()
print(now.strftime("%Y-%m-%d %H:%M"))

# Import everything (generally discouraged)
from math import *
print(floor(3.7))  # 3
  • import module — import the whole module, access with module.function()
  • from module import item — import specific items directly into your namespace
  • import module as alias — give a module a shorter name
  • from module import * — import everything (avoid this — it pollutes your namespace)
Try Imports
JavaScript
import math
print("Square root of 144:", math.sqrt(144))
print("Pi:", math.pi)
print("Ceiling of 4.3:", math.ceil(4.3))

import random
print("Random 1-10:", random.randint(1, 10))
print("Random choice:", random.choice(["apple", "banana", "cherry"]))
Notes
  • Always import modules at the top of your file. Follow the convention: standard library imports first, then third-party imports, then local imports, separated by blank lines.

Useful Built-in Modules and pip

Python's standard library includes many powerful modules out of the box. Some of the most commonly used are math, random, datetime, os, sys, and json.

For third-party packages not included in the standard library, you use pip — Python's package manager — to install them.

Example
# random module
import random
print(random.randint(1, 100))       # Random int between 1-100
print(random.choice(["a", "b", "c"]))# Random item from list
print(random.random())               # Random float 0.0 to 1.0

my_list = [1, 2, 3, 4, 5]
random.shuffle(my_list)
print(my_list)  # Shuffled in place

# datetime module
from datetime import datetime, timedelta
now = datetime.now()
print(now.strftime("%B %d, %Y"))  # March 29, 2026

future = now + timedelta(days=30)
print(f"30 days from now: {future.strftime('%Y-%m-%d')}")

# os module
import os
print(os.getcwd())                # Current working directory
print(os.path.exists("test.txt")) # Check if file exists

# Installing packages with pip (run in terminal)
# pip install requests
# pip install flask
# pip list (show installed packages)
# pip freeze > requirements.txt (save dependencies)
  • math — Mathematical functions (sqrt, sin, cos, pi, e)
  • random — Random number generation and selection
  • datetime — Date and time manipulation
  • os — Operating system interface (files, directories, paths)
  • json — JSON encoding and decoding
  • pip install package_name — Install a third-party package
  • pip freeze > requirements.txt — Save your project's dependencies
Try Built-in Modules
JavaScript
import random
import math
from datetime import datetime

# Math
print("Pi:", math.pi)
print("e:", math.e)

# Random
print("Random number:", random.randint(1, 100))
print("Coin flip:", random.choice(["Heads", "Tails"]))

# Date
now = datetime.now()
print("Today:", now.strftime("%B %d, %Y"))
Notes
  • Use virtual environments (venv) to isolate project dependencies. Create one with 'python -m venv myenv' and activate it before installing packages with pip.