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.

pythonmoduleslibrariesbeginnerimports

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 .py file 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:

Python
# 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:

Python
# main.py
import my_module

print(my_module.greet("Alice"))   # Hello, Alice! Welcome to Python.
print(my_module.person["age"])    # 25

The 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:

Python
import my_module as mm

print(mm.greet("Bob"))        # Hello, Bob! Welcome to Python.
print(mm.person["language"])  # Python

You 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

Python
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))   # 3

platform — System Information

Python
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

Python
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.0

datetime — Dates and Times

Python
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

Python
import math
print(math.pi)  # clear origin — you always see "math."

Import specific names

Python
from math import sqrt, pi
print(sqrt(64))  # 8.0 — no prefix needed

Import everything (use with caution)

Python
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.

LibraryPurposeInstall
NumPyFast numerical arrays and linear algebrapip install numpy
PandasData manipulation and analysispip install pandas
FlaskLightweight web frameworkpip install flask
TkinterDesktop GUI toolkitIncluded with Python
Pygame2D game developmentpip install pygame

Install any third-party library with pip, Python's package manager:

Python
# Terminal command — not Python code
# pip install pandas

Once installed, import it like any built-in module:

Python
import pandas as pd

data = pd.DataFrame({"name": ["Alice", "Bob"], "score": [92, 87]})
print(data)

🖼️ Visual Suggestion: A layered diagram — individual .py files (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:

Python
# 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

ConceptSyntaxWhen to Use
Import moduleimport mathAccess everything via math.xyz
Import with aliasimport numpy as npShorten frequent module references
Import specific namesfrom math import piOnly need a few items
Create a moduleSave code as utils.pyReuse logic across scripts
Install a librarypip install pandasNeed third-party functionality
Guard blockif __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.