Modules & Packages
Modules: familiar concept, simpler syntax
Section titled “Modules: familiar concept, simpler syntax”Python’s module system is conceptually identical to ESM: every .py file is a module, you import from other files or installed packages, and a package manager (pip) handles third-party dependencies. The syntax is cleaner — no export keyword, no default vs named export distinction.
Importing modules
Section titled “Importing modules”// TypeScript (ESM)import * as fs from "fs";import { join, dirname } from "path";import axios from "axios"; // default export from npm
// Named import with aliasimport { readFileSync as readFile } from "fs";
console.log(join("src", "main.ts"));# Pythonimport os # import whole modulefrom os.path import join, dirname # import specific namesimport math as m # import with aliasfrom math import sqrt, pi # import names directly
print(join("src", "main.py"))print(m.floor(3.7))print(sqrt(16))Packages vs modules
Section titled “Packages vs modules”In Python:
- A module is a single
.pyfile. - A package is a directory containing an
__init__.pyfile (Python 3.3+ also supports “namespace packages” without it).
// TypeScript project structure// src/// utils/// index.ts ← barrel export// string.ts// number.ts// main.ts
// main.tsimport { formatDate } from "./utils";import { capitalize } from "./utils/string";# Python package structure# src/# utils/# __init__.py ← marks it as a package# string_utils.py# number_utils.py# main.py
# main.pyfrom utils import format_date # from __init__.pyfrom utils.string_utils import capitalizepip — Python’s npm
Section titled “pip — Python’s npm”pip is the standard package installer. The Python ecosystem equivalent of package.json is requirements.txt (simple) or pyproject.toml (modern).
# npm / Node.js workflow# package.json declares dependencies
npm install # install all depsnpm install express # add a packagenpm install -D jest # add dev dependency
# import in codeimport express from "express";# pip / Python workflow# requirements.txt declares dependencies
pip install -r requirements.txt # install allpip install fastapi # add a packagepip install pytest --dev # dev dependency
# requirements.txt# fastapi==0.110.0# uvicorn==0.27.0# pytest==7.4.0
# Modern: use pyproject.toml + pip install -e .The standard library — batteries included
Section titled “The standard library — batteries included”Python ships a rich standard library. Many things you reach for npm to do are built-in:
| Python stdlib | npm equivalent |
|---|---|
json | JSON.parse / JSON.stringify |
os, pathlib | path, fs |
datetime | date-fns, dayjs |
re | regexp patterns |
collections | lodash data structures |
itertools | lodash iteration helpers |
http.server | express (very basic) |
unittest | jest (basic) |
Try it
Section titled “Try it”import osimport mathfrom collections import Counter
# os — path manipulationpath = os.path.join("src", "utils", "main.py")print(f"Path: {path}")
# math — built-in math functionsprint(f"sqrt(144) = {math.sqrt(144)}")print(f"pi = {math.pi:.4f}")print(f"ceil(2.3) = {math.ceil(2.3)}")
# collections.Counter — frequency countingwords = ["apple", "banana", "apple", "cherry", "banana", "apple"]freq = Counter(words)print(f"Word counts: {dict(freq)}")print(f"Most common: {freq.most_common(2)}")Loading Python runtime (first run only)…