Commonly used built-in modules | Python "built-in battery" quick start

Python takes "batteries included (built-in batteries, ready to use out of the box)" as its core design concept. The standard library covers more than 90% of daily development scenarios, without additionalpip installcan be called directly. This article has sorted out 10 types of high-frequency practical built-in modules, and attached minimalist code examples to help you quickly get started and solve practical problems.


1. System interaction module

sys: Working with the Python interpreter

sysAllows us to directly interact with the interpreter and system environment, such as viewing versions, processing command line parameters, or adjusting module search paths.

import sys

# 1. 获取解释器信息(快速定位兼容性问题)
print(f"Python 版本:{sys.version.split()[0]}")  # 只输出主版本号,更直观

# 2. 命令行参数(脚本名是 sys.argv[0])
if __name__ == "__main__":
    print(f"输入的参数:{sys.argv[1:]}")  # 跳过脚本名取实际参数

# 3. 强制退出(0 表示正常退出,非 0 为异常)
# sys.exit(1)

# 4. 动态添加模块搜索路径(临时生效,谨慎使用)
sys.path.append("/path/to/your/custom/modules")

os + pathlib: Manipulate files and directories gracefully

osA cross-platform system call interface is provided, but it is prone to errors when splicing paths. It is more recommended to use it firstpathlib(Python 3.4+), which uses object-oriented path design to make the code clearer and safer.

pathlibMinimalist usage

from pathlib import Path

# 1. 创建路径对象(自动适配 Windows / Linux / macOS 分隔符)
p = Path.cwd() / "data" / "user.txt"  # 用 / 拼接,自动处理转义

# 2. 路径属性
print(f"文件名:{p.name}")        # user.txt
print(f"无后缀名:{p.stem}")     # user
print(f"后缀:{p.suffix}")       # .txt
print(f"父目录:{p.parent}")     # ./data

# 3. 文件 / 目录检查
print(f"路径是否存在:{p.exists()}")
print(f"是否为文件:{p.is_file()}")
print(f"是否为目录:{p.parent.is_dir()}")

# 4. 读写文本(自动处理编码和关闭文件)
if not p.parent.exists():
    p.parent.mkdir(parents=True, exist_ok=True)  # 递归创建目录
p.write_text("Alice,25,Python")
print(p.read_text())  # 输出:Alice,25,Python

2. Date and time processing

datetime: Core date and time operations

datetimeThe module provides objects such as "date + time", "pure date", "pure time" and "time difference", and supports formatted output and string parsing.

from datetime import datetime, date, timedelta, timezone

# 1. 获取当前时间
now_local = datetime.now()                    # 本地时区的时间
now_utc = datetime.now(timezone.utc)          # 带时区信息的 UTC 时间(推荐后端存储使用)
print(f"本地时间:{now_local.strftime('%Y-%m-%d %H:%M:%S')}")

# 2. 创建特定时间
d = date(2024, 12, 31)
t_delta = timedelta(days=7, hours=2)          # 7 天 2 小时的时间差

# 3. 时间计算
next_week = now_local + t_delta
days_until_newyear = d - now_local.date()     # 返回 timedelta 对象
print(f"距离跨年还有:{days_until_newyear.days} 天")

# 4. 字符串解析为时间对象
str_time = "2024-05-20 13:14:00"
dt = datetime.strptime(str_time, "%Y-%m-%d %H:%M:%S")

time: Timestamps and performance timing

timeThe module is at a lower level, and common scenarios are to obtain Unix timestamp and accurately measure the time consuming of code snippets.

import time

# 1. Unix 时间戳(秒级,浮点数)
print(f"当前时间戳:{time.time():.0f}")

# 2. 让程序休眠(阻塞当前线程)
time.sleep(0.5)  # 休眠 0.5 秒

# 3. 高精度性能计时
start = time.perf_counter()
# 执行被测试的代码……
sum(range(1000000))
end = time.perf_counter()
print(f"代码耗时:{end - start:.3f} 秒")

3. Data serialization module

json: Common data exchange format

The preferred format when interacting with frontends and other languages.jsonOnly Python Basic types can be serialized (dictliststrintfloatboolNone)。

import json

data = {
    "name": "Alice",
    "age": 25,
    "skills": ["Python", "FastAPI"],
    "is_active": True
}

# 1. 序列化为格式化的 JSON 字符串
json_str = json.dumps(data, indent=2, ensure_ascii=False)  # ensure_ascii=False 保证中文正常显示
print(json_str)

# 2. 从 JSON 字符串反序列化为 Python 对象
data_loaded = json.loads(json_str)

# 3. 直接读写 JSON 文件
with open("user.json", "w", encoding="utf-8") as f:
    json.dump(data, f, indent=2, ensure_ascii=False)

with open("user.json", encoding="utf-8") as f:
    data_from_file = json.load(f)

pickle: Python proprietary serialization

You can serialize almost any Python object (class, function, instance, etc.), but only for Python use and do not load pickle files from unknown sources (a security risk).

import pickle

class User:
    def __init__(self, name):
        self.name = name

# 1. 序列化为字节流
user = User("Bob")
pickled_bytes = pickle.dumps(user)

# 2. 反序列化恢复对象
user_loaded = pickle.loads(pickled_bytes)
print(user_loaded.name)  # Bob

4. Mathematics and random number module

math:Basic mathematical operations

Provides commonly used mathematical functions and constants such as pi, trigonometric functions, logarithms, and combinatorial numbers.

import math

# 1. 常用常数
print(f"π:{math.pi:.4f}")
print(f"自然常数 e:{math.e:.4f}")

# 2. 常用函数
print(f"√16:{math.sqrt(16)}")
print(f"最大公约数:{math.gcd(48, 18)}")      # Python 3.5+
print(f"5 选 2 组合数:{math.comb(5, 2)}")   # Python 3.10+

random: Pseudo-random number generation

Commonly used for random sampling, shuffling order, generating test data, etc. If you need cryptographically secure random numbers in a production environment, please usesecretsmodule.

import random

# 1. 固定随机种子(调试时让“随机”结果可复现)
# random.seed(42)

# 2. 生成随机数
print(f"[0,1) 浮点数:{random.random():.2f}")
print(f"[1,6]    整数:{random.randint(1, 6)}")  # 模拟掷骰子

# 3. 对序列进行随机操作
fruits = ["苹果", "香蕉", "橙子", "葡萄"]
print(f"随机选 1 个:{random.choice(fruits)}")
print(f"随机选 2 个不重复:{random.sample(fruits, 2)}")
random.shuffle(fruits)  # 原地打乱列表
print(f"打乱后:{fruits}")

5. Data structure enhancement module

collections: Extended container type

Provides a Python base container (listdicttupleset) is an enhanced version that specifically solves performance or functional shortcomings in specific scenarios.

from collections import defaultdict, Counter, deque, namedtuple

# 1. defaultdict:访问不存在的键时自动给出默认值,无需手动判断
word_counts = defaultdict(int)
for word in ["a", "b", "a", "c", "a"]:
    word_counts[word] += 1
print(word_counts)  # defaultdict(<class 'int'>, {'a': 3, 'b': 1, 'c': 1})

# 2. Counter:快捷统计序列中每个元素的出现次数
cnt = Counter(["a", "b", "a", "c", "a"])
print(f"元素统计:{cnt}")
print(f"出现最多的 1 个:{cnt.most_common(1)}")  # [('a', 3)]

# 3. deque:双端队列,两端插入 / 删除都是 O(1),远比 list 快
dq = deque(maxlen=3)  # 固定长度为 3,满了会自动移除最早的
dq.append(1)
dq.append(2)
dq.append(3)
dq.append(4)  # 1 被自动移除
print(dq)  # deque([2, 3, 4], maxlen=3)

# 4. namedtuple:带字段名的元组,像类一样可读,但保持不可变性
Point = namedtuple("Point", ["x", "y"])
p = Point(10, 20)
print(f"x 坐标:{p.x},y 坐标:{p[1]}")  # 既可以用属性名,也可以用索引

6. Debugging and performance analysis module

logging:Structured logging

CompareprintA more professional debugging tool that supports level, output target, and unified format recording methods.

import logging

# 1. 全局配置(建议在程序入口只配置一次)
logging.basicConfig(
    level=logging.INFO,  # 只记录 INFO 及以上级别的日志(DEBUG / INFO / WARNING / ERROR / CRITICAL)
    format="%(asctime)s - %(levelname)s - %(message)s",
    handlers=[
        logging.FileHandler("app.log", encoding="utf-8"),  # 写入文件
        logging.StreamHandler()                            # 同时输出到控制台
    ]
)

logger = logging.getLogger(__name__)

# 2. 记录不同级别的日志
logger.debug("这是调试信息,默认不会显示")
logger.info("程序启动成功")
logger.warning("用户输入格式有误")
logger.error("数据库连接失败")

Summarize

The "battery built in" of the Python standard library is sufficient for most daily tasks. The benefits of using them first are obvious:

  1. No additional installation required, project dependencies are cleaner;
  2. Proven stability, guaranteed performance and reliability;
  3. Naturally cross-platform, no need to care about system differences;
  4. The official documentation is complete, making it easier to get started and troubleshoot.

Only when the standard library really cannot meet the needs (such as complex HTTP requests usingrequests, for large-scale data processingpandas), and then introducing third-party libraries will make your project more robust and efficient.