The Python standard library
***************************

This library reference manual describes the standard library
distributed with Python. It also describes some of the optional
components that are commonly included in Python distributions.

Elsewhere, Python 语言参考手册 describes the exact syntax and
semantics of the Python language, and Python built-ins reference
describes the built-in functions.

Python's standard library is extensive, offering a wide range of
facilities as indicated by the long table of contents listed below.
The library contains built-in modules (written in C) that provide
access to system functionality such as file I/O that would otherwise
be inaccessible to Python programmers, as well as modules written in
Python that provide standardized solutions for many problems that
occur in everyday programming. Some of these modules are explicitly
designed to encourage and enhance the portability of Python programs
by abstracting away platform-specifics into platform-neutral APIs.

Windows 版本的 Python 安装程序通常包含整个标准库，往往还包含许多额外组
件。对于类 Unix 操作系统，Python 通常会分成一系列的软件包，因此可能需
要使用操作系统所提供的包管理工具来获取部分或全部可选组件。

在标准库以外，还存在一个由数十万个组件（从单独的程序和模块到软件包以及
完整的应用程序开发框架）组成的活跃合集，可以从 Python 包索引 获取。

* 概述

  * 可用性注释

* 文本处理服务

  * "string" --- 常见的字符串操作

  * "string.templatelib" --- 对模板字符串字面值的支持

  * "re" --- 正则表达式操作

  * "difflib" --- 计算差异的辅助工具

  * "textwrap" --- 文本自动换行与填充

  * "unicodedata" --- Unicode 数据库

  * "stringprep" --- 因特网字符串预处理

  * "readline" --- GNU readline 接口

  * "rlcompleter" --- 用于 GNU readline 的补全函数

* 二进制数据服务

  * "struct" --- 将字节串解读为打包的二进制数据

  * "codecs" --- 编解码器注册和相关基类

* 数据类型

  * "datetime" --- 基本日期和时间类型

  * "zoneinfo" --- IANA 时区支持

  * "calendar" --- 通用日历相关函数

  * "collections" --- 容器数据类型

  * "collections.abc" --- 容器的抽象基类

  * "heapq" --- 堆队列算法

  * "bisect" --- 数组二分算法

  * "array" --- 高效的数值数组

  * "weakref" --- 弱引用

  * "types" --- 动态类型创建和内置类型名称

  * "copy" --- 浅层及深层拷贝操作

  * "pprint" --- 数据美化输出

  * "reprlib" --- 替代性 "repr()" 实现

  * "enum" --- 对枚举的支持

  * "graphlib" --- 操作类似图的结构的功能

* 数字和数学模块

  * "numbers" --- 数字抽象基类

  * "math" --- 数学函数

  * "math.integer" --- integer-specific mathematics functions

  * "cmath" --- 针对复数的数学函数

  * "decimal" --- 十进制定点和浮点算术

  * "fractions" --- 有理数

  * "random" --- 生成伪随机数

  * "statistics" --- 数学统计函数

* 函数式编程模块

  * "itertools" --- 为高效循环创建迭代器的函数

  * "functools" —— 高阶函数，以及可调用对象上的操作

  * "operator" --- 标准运算符对应函数

* 文件和目录访问

  * "pathlib" --- 面向对象的文件系统路径

  * "os.path" --- 常用的路径操作

  * "stat" --- 解释 "stat()" 的结果

  * "filecmp" --- 文件和目录比较

  * "tempfile" --- 生成临时文件和目录

  * "glob" --- Unix 风格的路径名模式扩展

  * "fnmatch" --- Unix 文件名模式匹配

  * "linecache" --- 随机访问文本行

  * "shutil" --- 高层级文件操作

* 数据持久化

  * "pickle" --- Python 对象序列化

  * "copyreg" --- 注册 "pickle" 支持函数

  * "shelve" --- Python 对象持久化

  * "marshal" --- 内部 Python 对象序列化

  * "dbm" --- Unix "数据库" 接口

  * "sqlite3" --- SQLite 数据库的 DB-API 2.0 接口

* 数据压缩和存档

  * "compression" 包

  * "compression.zstd" --- 与 Zstandard 格式兼容的压缩

  * "zlib" --- 与 **gzip** 兼容的压缩

  * "gzip" --- 对 **gzip** 文件的支持

  * "bz2" --- 对 **bzip2** 压缩算法的支持

  * "lzma" --- 使用 LZMA 算法进行压缩

  * "zipfile" --- 操作 ZIP 归档文件

  * "tarfile" --- 读写 tar 归档文件

* 文件格式

  * "csv" --- CSV 文件读写

  * "configparser" --- 配置文件解析器

  * "tomllib" --- 解析 TOML 文件

  * "netrc" --- netrc 文件处理

  * "plistlib" --- 生成与解析 Apple ".plist" 文件

* 加密服务

  * "hashlib" --- 安全哈希与消息摘要

  * "hmac" --- 用于消息验证的密钥哈希

  * "secrets" --- 生成管理密码的安全随机数

* 通用操作系统服务

  * "os" --- 多种操作系统接口

  * "io" --- 处理流的核心工具

  * "time" --- 时间的访问和转换

  * "logging" --- Python 的日志记录工具

  * "logging.config" --- 日志记录配置

  * "logging.handlers" --- 日志处理器

  * "platform" ---  访问底层平台的标识数据

  * "errno" --- 标准 errno 系统符号

  * "ctypes" --- Python 的外部函数库

* 命令行界面库

  * "argparse" --- 用于命令行选项、参数和子命令的解析器

  * "optparse" --- 命令行选项的解析器

  * "getpass" --- 可移植的密码输入

  * "fileinput" --- 迭代来自多个输入流的行

  * "curses" --- 字符单元显示的终端处理

  * "curses.textpad" --- Text input widget for curses programs

  * "curses.ascii" --- 用于 ASCII 字符的工具

  * "curses.panel" --- 针对 curses 的面板栈扩展

  * "cmd" --- 对面向行的命令解释器的支持

* 并发执行

  * "threading" --- 基于线程的并行

  * "multiprocessing" --- 基于进程的并行

  * "multiprocessing.shared_memory" --- 可跨进程直接访问的共享内存

  * "concurrent" 包

  * "concurrent.futures" --- 启动并行任务

  * "concurrent.interpreters" --- 同一进程中的多个解释器

  * "subprocess" --- 子进程管理

  * "sched" --- 事件调度器

  * "queue" --- 同步队列类

  * "contextvars" --- 上下文变量

  * "_thread" --- 低层级多线程 API

* 网络和进程间通信

  * "asyncio" --- 异步 I/O

  * "socket" --- 低层级的网络接口

  * "ssl" --- 套接字对象的 TLS/SSL 包装器

  * "select" --- 等待 I/O 完成

  * "selectors" --- 高层级 I/O 复用

  * "signal" --- 设置异步事件处理器

  * "mmap" --- 内存映射文件支持

* 互联网数据处理

  * "email" --- 电子邮件与 MIME 处理包

  * "json" --- JSON 编码器和解码器

  * "mailbox" --- 操纵多种格式的邮箱

  * "mimetypes" --- 将文件名映射到 MIME 类型

  * "base64" --- Base16, Base32, Base64, Base85 数据编码

  * "binascii" --- 在二进制数据和 ASCII 之间进行转换

  * "quopri" --- 编码与解码 MIME 转码的可打印数据

* 结构化标记处理工具

  * "html" --- 超文本标记语言支持

  * "html.parser" --- 简单的 HTML 和 XHTML 解析器

  * "html.entities" --- HTML 一般实体的定义

  * XML 处理模块

  * "xml.etree.ElementTree" --- ElementTree XML API

  * "xml.dom" --- 文档对象模型 API

  * "xml.dom.minidom" --- 最小化的 DOM 实现

  * "xml.dom.pulldom" --- 对构建部分 DOM 树的支持

  * "xml.sax" --- SAX2 解析器支持

  * "xml.sax.handler" --- SAX 处理器的基类

  * "xml.sax.saxutils" --- SAX 工具集

  * "xml.sax.xmlreader" --- 用于 XML 解析器的接口

  * "xml.parsers.expat" --- 使用 Expat 进行快速 XML 解析

* 互联网协议和支持

  * "webbrowser" --- 方便的 Web 浏览器控制工具

  * "wsgiref" --- WSGI 工具和参考实现

  * "urllib" --- URL 处理模块

  * "urllib.request" --- 用于打开 URL 的可扩展库

  * "urllib.response" --- urllib 使用的响应类

  * "urllib.parse" --- 将 URL 解析为组件

  * "urllib.error" --- 由 urllib.request 引发的异常类

  * "urllib.robotparser" ---  用于 robots.txt 的解析器

  * "http" --- HTTP 模块

  * "http.client" --- HTTP 协议客户端

  * "ftplib" --- FTP 协议客户端

  * "poplib" --- POP3 协议客户端

  * "imaplib" --- IMAP4 协议客户端

  * "smtplib" --- SMTP 协议客户端

  * "uuid" --- 根据 **RFC 9562** 定义的 UUID 对象

  * "socketserver" --- 用于网络服务器的框架

  * "http.server" --- HTTP 服务器

  * "http.cookies" --- HTTP 状态管理

  * "http.cookiejar" --- HTTP 客户端的 Cookie 处理

  * "xmlrpc" --- XMLRPC 服务端与客户端模块

  * "xmlrpc.client" --- XML-RPC 客户端访问

  * "xmlrpc.server" --- 基本 XML-RPC 服务器

  * "ipaddress" --- IPv4/IPv6 操作库

* 多媒体服务

  * "wave" --- 读写 WAV 文件

  * "colorsys" --- 颜色系统间的转换

* 国际化

  * "gettext" --- 多语种国际化服务

  * "locale" --- 国际化服务

* 使用 Tk 创建图形用户界面

  * "tkinter" --- Tcl/Tk 的 Python 接口

  * "tkinter.colorchooser" --- 颜色选择对话框

  * "tkinter.font" --- Tkinter 字体包装器

  * Tkinter 对话框

  * "tkinter.messagebox" --- Tkinter 消息提示

  * "tkinter.scrolledtext" --- 滚动文本控件

  * "tkinter.dnd" --- 拖放操作支持

  * "tkinter.ttk" --- Tk 带主题的控件

  * IDLE --- Python 编辑器和 shell

  * "turtle" --- 海龟绘图

* 开发工具

  * "typing" --- 对类型提示的支持

  * "pydoc" --- 文档生成器和在线帮助系统

  * Python 开发模式

  * "doctest" --- 测试交互式的 Python 示例

  * "unittest" --- 单元测试框架

  * "unittest.mock" --- 模拟对象库

  * "unittest.mock" --- 新手入门

  * "test" --- Python 回归测试包

  * "test.support" --- 针对 Python 测试套件的工具

  * "test.support.socket_helper" --- 针对套接字测试的工具

  * "test.support.script_helper" --- 用于 Python 执行测试的工具

  * "test.support.bytecode_helper" --- 用于测试正确字节码生成的支持工
    具

  * "test.support.threading_helper" --- 用于线程测试的工具

  * "test.support.os_helper" --- 用于操作系统测试的工具

  * "test.support.import_helper" --- 用于导入测试的工具Utilities for
    import tests

  * "test.support.warnings_helper" --- 用于警告测试的工具Utilities for
    warnings tests

* 调试与性能分析

  * 审计事件表

  * "bdb" --- 调试器框架

  * "faulthandler" --- 转储 Python 回溯信息

  * "pdb" --- The Python Debugger

  * "profiling" --- Python 性能分析器

  * "pstats" --- 性能分析器的统计信息

  * "timeit" --- 测量小代码片段的执行时间

  * "trace" --- 跟踪或记录 Python 语句的执行

  * "tracemalloc" --- 跟踪内存分配

* 软件打包和分发

  * "ensurepip" --- 初始设置 "pip" 安装器

  * "venv" --- 虚拟环境的创建

  * "zipapp" --- 管理可执行的 Python zip 归档文件

* Python 运行时服务

  * "sys" --- 系统相关的形参和函数

  * "sys.monitoring" --- 执行事件监测

  * "sysconfig" --- 提供对 Python 配置信息的访问

  * "builtins" --- 内置对象

  * "__main__" --- 最高层级代码环境

  * "warnings" --- 警告信息控制

  * "dataclasses" --- 数据类

  * "contextlib" --- 为 "with"语句上下文提供的工具

  * "abc" --- 抽象基类

  * "atexit" --- 退出处理器

  * "traceback" --- 打印或读取栈回溯信息

  * "__future__" --- Future 语句定义

  * "gc" --- 垃圾回收器接口

  * "inspect" --- 检查活动对象

  * "annotationlib" --- 用于内省注解的功能

  * "site" --- 站点专属的配置钩子

* 自定义 Python 解释器

  * "code" --- 解释器基类

  * "codeop" --- 编译 Python 代码

* 导入模块

  * "zipimport" --- 从 Zip 归档导入模块

  * "pkgutil" --- 包扩展工具

  * "modulefinder" --- 查找脚本使用的模块

  * "runpy" --- 查找并执行 Python 模块

  * "importlib" --- "import" 的实现

  * "importlib.resources" -- 包资源的读取、打开和访问

  * "importlib.resources.abc" -- 资源的抽象基类

  * "importlib.metadata" -- 访问软件包元数据

  * "sys.path" 模块搜索路径的初始化

* Python 语言服务

  * "ast" --- 抽象语法树

  * "symtable" --- 访问编译器的符号表

  * "token" --- 用于 Python 解析树的常量

  * "keyword" --- 检验 Python 关键字

  * "tokenize" --- Python 源代码的分词器

  * "tabnanny" --- 检测有歧义的缩进

  * "pyclbr" --- Python 模块浏览器支持

  * "py_compile" --- 编译 Python 源文件

  * "compileall" --- 字节编译 Python 库

  * "dis" --- Python 字节码反汇编器

  * "pickletools" --- pickle 开发者工具

* Windows 系统相关模块

  * "msvcrt" --- 来自 MS VC++ 运行时的有用例程

  * "winreg" --- Windows 注册表访问

  * "winsound" --- 针对 Windows 的声音播放接口

* Unix 专属服务

  * "shlex" --- 简单词法分析

  * "posix" --- 最常见的 POSIX 系统调用

  * "pwd" --- 密码数据库

  * "grp" --- 组数据库

  * "termios" --- POSIX 风格的 tty 控制

  * "tty" --- 终端控制函数

  * "pty" --- 伪终端工具

  * "fcntl" --- "fcntl" 和 "ioctl" 系统调用

  * "resource" --- 资源使用信息

  * "syslog" --- Unix syslog 库例程

* 模块命令行界面（CLI）

* 被取代的模块

  * "getopt" --- C 风格的命令行选项解析器

  * "profile" --- 纯 Python 性能分析器

* 移除的模块

* 安全考量
