March 11, 2026 · All About CS
Python Modules and Libraries: Organize and Supercharge Your Code
Understand how Python modules and libraries work — learn to create your own modules, import built-in ones like math and platform, and discover powerful third-party libraries.
Python Modules and Libraries: Organize and Supercharge Your Code
As your programs grow, stuffing everything into a single file becomes unmanageable. Modules solve this by letting you split code into separate, reusable files — and libraries take the concept further by bundling related modules into powerful toolkits that can save you weeks of work.
Key Takeaways
- A module is simply a
.pyfile containing functions, classes, and variables you can reuse across projects - You can create your own modules or leverage Python's rich collection of built-in modules
- Aliases keep your code concise when module names are long
- Different import styles give you control over what enters your namespace
- Libraries are collections of modules — install third-party ones with
pip - The
if __name__ == "__main__"pattern prevents code from running on import
What Is a Module?
A module is any Python file (ending in .py) that contains reusable code — functions, classes, constants, or any combination of these. Think of it as a toolkit: you build it once, then reach for it whenever you need its tools.
Python itself ships with hundreds of modules (the standard library), and the community has published over 400,000 more on PyPI.
🖼️ Visual Suggestion: A diagram showing a single Python file (module) containing functions and variables, with arrows pointing to multiple other scripts that import it.
Creating Your Own Module
Any .py file is already a module. Create a file called my_module.py with some reusable code:
# my_module.py — a simple custom module
def greet(name):
"""Return a personalised greeting."""
return f"Hello, {name}! Welcome to Python."
person = {
"name": "Alice",
"age": 25,
"language": "Python"
}Now import and use it from another script in the same directory:
# main.py
import my_module
print(my_module.greet("Alice")) # Hello, Alice! Welcome to Python.
print(my_module.person["age"]) # 25The dot notation (my_module.greet) keeps it clear where each function or variable originates — a big win for readability in larger projects.
Module Aliases
Long module names can clutter your code. The as keyword lets you assign a shorter alias:
import my_module as mm
print(mm.greet("Bob")) # Hello, Bob! Welcome to Python.
print(mm.person["language"]) # PythonYou will see aliases everywhere in the Python ecosystem — numpy becomes np, pandas becomes pd, and matplotlib.pyplot becomes plt.
Built-in Modules
Python's standard library is one of the language's greatest strengths. Here are four modules you will reach for regularly.
math — Mathematical Functions
import math
print(math.sqrt(144)) # 12.0
print(math.pow(2, 10)) # 1024.0
print(math.pi) # 3.141592653589793
print(math.floor(3.9)) # 3platform — System Information
import platform
print(platform.system()) # e.g. "Windows", "Linux", "Darwin"
print(platform.processor()) # e.g. "Intel64 Family 6 ..."
print(platform.python_version()) # e.g. "3.12.0"random — Random Number Generation
import random
print(random.randint(1, 100)) # random int between 1 and 100
print(random.choice(["red", "green", "blue"])) # random pick from a list
print(random.random()) # random float between 0.0 and 1.0datetime — Dates and Times
from datetime import date, datetime
print(date.today()) # e.g. 2026-03-11
print(datetime.now().strftime("%I:%M %p")) # e.g. 02:30 PM🖼️ Visual Suggestion: A grid of four cards — one per built-in module above — each showing the module name, a one-line description, and its most-used function.
Import Styles
Python offers several ways to bring module code into your script. Each has trade-offs.
Import the entire module
import math
print(math.pi) # clear origin — you always see "math."Import specific names
from math import sqrt, pi
print(sqrt(64)) # 8.0 — no prefix neededImport everything (use with caution)
from math import *
print(ceil(4.2)) # works, but where did ceil come from?Wildcard imports (*) dump every public name into your namespace, which can silently shadow your own variables and make debugging painful. Prefer explicit imports in production code.
Libraries: Collections of Modules
A library is a collection of related modules distributed as a single package. While a module is a single file, a library can contain dozens of modules organized into sub-packages.
| Library | Purpose | Install |
|---|---|---|
| NumPy | Fast numerical arrays and linear algebra | pip install numpy |
| Pandas | Data manipulation and analysis | pip install pandas |
| Flask | Lightweight web framework | pip install flask |
| Tkinter | Desktop GUI toolkit | Included with Python |
| Pygame | 2D game development | pip install pygame |
Install any third-party library with pip, Python's package manager:
# Terminal command — not Python code
# pip install pandasOnce installed, import it like any built-in module:
import pandas as pd
data = pd.DataFrame({"name": ["Alice", "Bob"], "score": [92, 87]})
print(data)🖼️ Visual Suggestion: A layered diagram — individual
.pyfiles (modules) at the bottom, grouped into packages in the middle, and a library label wrapping the entire stack.
The if __name__ == "__main__" Pattern
When Python runs a file directly, it sets the special variable __name__ to "__main__". When the file is imported as a module, __name__ is set to the module's name instead. This lets you include code that only executes during direct runs:
# my_module.py
def greet(name):
return f"Hello, {name}!"
if __name__ == "__main__":
# This block runs ONLY when you execute: python my_module.py
print(greet("Developer"))This pattern is invaluable for adding quick tests or demos to a module without affecting other scripts that import it.
Quick Reference
| Concept | Syntax | When to Use |
|---|---|---|
| Import module | import math | Access everything via math.xyz |
| Import with alias | import numpy as np | Shorten frequent module references |
| Import specific names | from math import pi | Only need a few items |
| Create a module | Save code as utils.py | Reuse logic across scripts |
| Install a library | pip install pandas | Need third-party functionality |
| Guard block | if __name__ == "__main__": | Prevent code from running on import |
Up next: we'll explore file handling in Python — reading, writing, and managing data stored on disk.