Skip to content

Modules & Packages

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.

TypeScript
// TypeScript (ESM)
import * as fs from "fs";
import { join, dirname } from "path";
import axios from "axios"; // default export from npm
// Named import with alias
import { readFileSync as readFile } from "fs";
console.log(join("src", "main.ts"));
Python
# Python
import os # import whole module
from os.path import join, dirname # import specific names
import math as m # import with alias
from math import sqrt, pi # import names directly
print(join("src", "main.py"))
print(m.floor(3.7))
print(sqrt(16))

In Python:

  • A module is a single .py file.
  • A package is a directory containing an __init__.py file (Python 3.3+ also supports “namespace packages” without it).
TypeScript
// TypeScript project structure
// src/
// utils/
// index.ts ← barrel export
// string.ts
// number.ts
// main.ts
// main.ts
import { formatDate } from "./utils";
import { capitalize } from "./utils/string";
Python
# Python package structure
# src/
# utils/
# __init__.py ← marks it as a package
# string_utils.py
# number_utils.py
# main.py
# main.py
from utils import format_date # from __init__.py
from utils.string_utils import capitalize

pip is the standard package installer. The Python ecosystem equivalent of package.json is requirements.txt (simple) or pyproject.toml (modern).

npm (Node.js)
# npm / Node.js workflow
# package.json declares dependencies
npm install # install all deps
npm install express # add a package
npm install -D jest # add dev dependency
# import in code
import express from "express";
pip (Python)
# pip / Python workflow
# requirements.txt declares dependencies
pip install -r requirements.txt # install all
pip install fastapi # add a package
pip 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 stdlibnpm equivalent
jsonJSON.parse / JSON.stringify
os, pathlibpath, fs
datetimedate-fns, dayjs
reregexp patterns
collectionslodash data structures
itertoolslodash iteration helpers
http.serverexpress (very basic)
unittestjest (basic)
import os
import math
from collections import Counter
# os — path manipulation
path = os.path.join("src", "utils", "main.py")
print(f"Path: {path}")
# math — built-in math functions
print(f"sqrt(144) = {math.sqrt(144)}")
print(f"pi = {math.pi:.4f}")
print(f"ceil(2.3) = {math.ceil(2.3)}")
# collections.Counter — frequency counting
words = ["apple", "banana", "apple", "cherry", "banana", "apple"]
freq = Counter(words)
print(f"Word counts: {dict(freq)}")
print(f"Most common: {freq.most_common(2)}")
What is the Python equivalent of npm?
In Python, which file makes a directory a package?
Which import style brings only `sqrt` into the current namespace?