On this page
article
Standard Library Quick Reference
Quick reference for Python most-used standard library modules — os, sys, pathlib, json, datetime, collections, and more.
Python’s standard library is extensive. These are the modules you’ll use most often.
os & sys
import os, sys
os.environ["HOME"] # environment variables
os.getcwd(), os.chdir(path)
os.listdir(path)
os.path.join("a", "b", "c")
os.makedirs("dir/sub", exist_ok=True)
os.remove("file"), os.rename("old", "new")
sys.argv # command-line arguments
sys.exit(1) # exit with code
sys.path # module search paths
sys.platform # 'darwin', 'linux', 'win32'
pathlib (Preferred for Paths)
from pathlib import Path
p = Path("data/file.txt")
p.exists(), p.is_file(), p.is_dir()
p.read_text(), p.write_text("content")
p.read_bytes(), p.write_bytes(b"data")
p.parent, p.name, p.stem, p.suffix
p.glob("*.py"), p.rglob("**/*.py")
Path.home(), Path.cwd()
json
import json
json.dumps(obj, indent=2) # Python → string
json.loads(string) # string → Python
json.dump(obj, file)
json.load(file)
datetime
from datetime import datetime, date, timedelta, timezone
now = datetime.now()
today = date.today()
utc = datetime.now(timezone.utc)
delta = timedelta(days=7, hours=3)
future = now + delta
now.strftime("%Y-%m-%d %H:%M:%S")
datetime.fromisoformat("2024-06-15T10:30:00")
collections
from collections import (
defaultdict, Counter, deque,
namedtuple, OrderedDict, ChainMap,
)
dd = defaultdict(list)
dd["key"].append(val)
counts = Counter("abracadabra")
counts.most_common(3)
queue = deque([1, 2, 3])
queue.append(4), queue.popleft()
Point = namedtuple("Point", ["x", "y"])
p = Point(1, 2)
itertools
from itertools import (
chain, combinations, permutations,
groupby, islice, cycle, repeat,
zip_longest,
)
chain([1, 2], [3, 4]) # 1, 2, 3, 4
combinations([1,2,3], 2) # (1,2), (1,3), (2,3)
groupby(sorted(data), key=func)
islice(range(100), 5, 10) # 5..9
functools
from functools import lru_cache, partial, wraps, reduce
@lru_cache(maxsize=128)
def fib(n): ...
add_five = partial(add, 5)
reduce(lambda a, b: a + b, [1, 2, 3])
re (Regular Expressions)
import re
re.search(r"\d+", text) # first match
re.findall(r"\w+", text) # all matches
re.sub(r"\d+", "X", text) # replace
re.split(r"\s+", text) # split
re.compile(r"pattern") # compile once
argparse
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("name")
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument("-n", type=int, default=1)
args = parser.parse_args()
logging
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.debug/info/warning/error/critical("message")
logger.exception("with traceback")
unittest / pytest
import unittest
class TestMath(unittest.TestCase):
def test_add(self):
self.assertEqual(add(1, 2), 3)
# pytest style
def test_add():
assert add(1, 2) == 3
hashlib & secrets
import hashlib, secrets
hashlib.sha256(b"data").hexdigest()
secrets.token_hex(16) # random token
secrets.compare_digest(a, b) # timing-safe compare
urllib
from urllib.parse import urlparse, urlencode, quote
from urllib.request import urlopen
parsed = urlparse("https://example.com/path?q=1")
params = urlencode({"key": "value"})
sqlite3
import sqlite3
conn = sqlite3.connect("app.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE id = ?", (user_id,))
rows = cursor.fetchall()
conn.commit()
conn.close()
threading & multiprocessing
import threading, multiprocessing
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
with ThreadPoolExecutor(max_workers=4) as ex:
results = list(ex.map(func, items))
Related
The official library index lists every standard module.