Python 语言参考 描述了 Python 语言的具体语法和语义,这份库参考则介绍了与 Python 一同发行的标准库。它还描述了通常包含在 Python 发行版中的一些可选组件。
Python 标准库非常庞大,所提供的组件涉及范围十分广泛,正如以下内容目录所显示的。这个库包含了多个内置模块 (以 C 编写),Python 程序员必须依靠它们来实现系统级功能,例如文件 I/O,此外还有大量以 Python 编写的模块,提供了日常编程中许多问题的标准解决方案。其中有些模块经过专门设计,通过将特定平台功能抽象化为平台中立的 API 来鼓励和加强 Python 程序的可移植性。
Windows 版本的 Python 安装程序通常包含整个标准库,往往还包含许多额外组件。对于类 Unix 操作系统,Python 通常会分成一系列的软件包,因此可能需要使用操作系统所提供的包管理工具来获取部分或全部可选组件。
在这个标准库以外还存在成千上万并且不断增加的其他组件 (从单独的程序、模块、软件包直到完整的应用开发框架),访问 Python 包索引 即可获取这些第三方包。
site
模块添加的常量and
, or
, not
int
, float
, complex
list
, tuple
, range
str
bytes
, bytearray
, memoryview
set
, frozenset
dict
string
— 常见的字符串操作re
— 正则表达式操作difflib
— 计算差异的辅助工具textwrap
— 文本自动换行与填充unicodedata
— Unicode 数据库stringprep
— 因特网字符串预备readline
— GNU readline 接口rlcompleter
— GNU readline 的补全函数struct
— 将字节串解读为打包的二进制数据codecs
— 编解码器注册和相关基类datetime
— 基本的日期和时间类型calendar
— 日历相关函数collections
— 容器数据类型collections.abc
— 容器的抽象基类heapq
— 堆队列算法bisect
— 数组二分查找算法array
— 高效的数值数组weakref
— 弱引用types
— 动态类型创建和内置类型名称copy
— 浅层 (shallow) 和深层 (deep) 复制操作pprint
— 数据美化输出reprlib
— 另一种 repr()
实现enum
— 对枚举的支持numbers
— 数字的抽象基类math
— 数学函数cmath
— 关于复数的数学函数decimal
— 十进制定点和浮点运算fractions
— 分数random
— 生成伪随机数statistics
— 数学统计函数itertools
— 为高效循环而创建迭代器的函数functools
— 高阶函数和可调用对象上的操作operator
— 标准运算符替代函数pathlib
— 面向对象的文件系统路径os.path
— 常用路径操作fileinput
— 迭代来自多个输入流的行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 接口模块zlib
— 与 gzip 兼容的压缩gzip
— 对 gzip 格式的支持bz2
— 对 bzip2 压缩算法的支持lzma
— 用 LZMA 算法压缩zipfile
— 使用ZIP存档tarfile
— 读写tar归档文件csv
— CSV 文件读写configparser
— 配置文件解析器netrc
— netrc 文件处理xdrlib
— 编码与解码 XDR 数据plistlib
— 生成与解析 Mac OS X .plist
文件hashlib
— 安全哈希与消息摘要hmac
— 基于密钥的消息验证secrets
— 生成安全随机数字用于管理密码os
— 多种操作系统接口io
— 处理流的核心工具time
— 时间的访问和转换argparse
— 命令行选项、参数和子命令解析器getopt
— C-style parser for command line optionslogging
— Python 的日志记录工具logging.config
— 日志记录配置logging.handlers
— 日志处理getpass
— 便携式密码输入工具curses
— 终端字符单元显示的处理curses.textpad
— Text input widget for curses programscurses.ascii
— Utilities for ASCII characterscurses.panel
— A panel stack extension for cursesplatform
— 获取底层平台的标识数据errno
— Standard errno system symbolsctypes
— Python 的外部函数库threading
— 基于线程的并行multiprocessing
— 基于进程的并行multiprocessing.shared_memory
— 可从进程直接访问的共享内存concurrent
包concurrent.futures
— 启动并行任务subprocess
— 子进程管理sched
— 事件调度器queue
— 一个同步的队列类_thread
— 底层多线程 API_dummy_thread
— _thread
的替代模块dummy_threading
— 可直接替代 threading
模块。contextvars
— Context Variables
asyncio
— 异步 I/Osocket
— 底层网络接口ssl
— 套接字对象的TLS/SSL封装select
— 等待 I/O 完成selectors
— 高级 I/O 复用库asyncore
— 异步socket处理器asynchat
— 异步 socket 指令/响应 处理器signal
— 设置异步事件处理程序mmap
— 内存映射文件支持email
— 电子邮件与 MIME 处理包json
— JSON 编码和解码器mailcap
— Mailcap 文件处理mailbox
— Manipulate mailboxes in various formatsmimetypes
— Map filenames to MIME typesbase64
— Base16, Base32, Base64, Base85 数据编码binhex
— 对binhex4文件进行编码和解码binascii
— 二进制和 ASCII 码互转quopri
— 编码与解码经过 MIME 转码的可打印数据uu
— 对 uuencode 文件进行编码与解码html
— 超文本标记语言支持html.parser
— 简单的 HTML 和 XHTML 解析器html.entities
— HTML 一般实体的定义xml.etree.ElementTree
— ElementTree XML APIxml.dom
— The Document Object Model APIxml.dom.minidom
— Minimal DOM implementationxml.dom.pulldom
— Support for building partial DOM treesxml.sax
— Support for SAX2 parsersxml.sax.handler
— Base classes for SAX handlersxml.sax.saxutils
— SAX 工具集xml.sax.xmlreader
— Interface for XML parsersxml.parsers.expat
— Fast XML parsing using Expatwebbrowser
— 方便的Web浏览器控制器cgi
— Common Gateway Interface supportcgitb
— 用于 CGI 脚本的回溯管理器wsgiref
— WSGI Utilities and Reference Implementationurllib
— URL 处理模块urllib.request
— 用于打开 URL 的可扩展库urllib.response
— urllib 使用的 Response 类urllib.parse
— Parse URLs into componentsurllib.error
— urllib.request 引发的异常类urllib.robotparser
— robots.txt 语法分析程序http
— HTTP 模块http.client
— HTTP 协议客户端ftplib
— FTP 协议客户端poplib
— POP3 protocol clientimaplib
— IMAP4 protocol clientnntplib
— NNTP protocol clientsmtplib
—SMTP协议客户端smtpd
— SMTP 服务器telnetlib
— Telnet clientuuid
— UUID objects according to RFC 4122socketserver
— A framework for network servershttp.server
— HTTP 服务器http.cookies
— HTTP状态管理http.cookiejar
—— HTTP 客户端的 Cookie 处理xmlrpc
— XMLRPC 服务端与客户端模块xmlrpc.client
— XML-RPC client accessxmlrpc.server
— Basic XML-RPC serversipaddress
— IPv4/IPv6 manipulation libraryaudioop
— Manipulate raw audio dataaifc
— Read and write AIFF and AIFC filessunau
— 读写 Sun AU 文件wave
— 读写WAV格式文件chunk
— 读取 IFF 分块数据colorsys
— 颜色系统间的转换imghdr
— 推测图像类型sndhdr
— 推测声音文件的类型ossaudiodev
— Access to OSS-compatible audio devicesgettext
— 多语种国际化服务locale
— 国际化服务turtle
— 海龟绘图cmd
— 支持面向行的命令解释器shlex
— Simple lexical analysistkinter
— Tcl/Tk的Python接口tkinter.ttk
— Tk主题小部件tkinter.tix
— Extension widgets for Tktkinter.scrolledtext
— 滚动文字控件typing
— 类型标注支持pydoc
— Documentation generator and online help systemdoctest
— 测试交互性的Python示例unittest
— 单元测试框架unittest.mock
— mock对象库unittest.mock
上手指南test
— Regression tests package for Pythontest.support
— Utilities for the Python test suitetest.support.script_helper
— Utilities for the Python execution testsbdb
— Debugger frameworkfaulthandler
— Dump the Python tracebackpdb
— Python的调试器timeit
— 测量小代码片段的执行时间trace
— Trace or track Python statement executiontracemalloc
— 跟踪内存分配distutils
— 构建和安装 Python 模块ensurepip
— Bootstrapping the pip
installervenv
— 创建虚拟环境zipapp
— Manage executable Python zip archivessys
— 系统相关的参数和函数sysconfig
— Provide access to Python’s configuration informationbuiltins
— 内建对象__main__
— 顶层脚本环境warnings
— Warning controldataclasses
— 数据类contextlib
— Utilities for with
-statement contextsabc
— 抽象基类atexit
— 退出处理器traceback
— 打印或检索堆栈回溯__future__
— Future 语句定义gc
— 垃圾回收器接口inspect
— 检查对象site
—— 指定域的配置钩子code
— 解释器基类codeop
— 编译Python代码zipimport
— 从 Zip 存档中导入模块pkgutil
— 包扩展工具modulefinder
— 查找脚本使用的模块runpy
— Locating and executing Python modulesimportlib
— import
的实现parser
— Access Python parse treesast
— 抽象语法树symtable
— Access to the compiler’s symbol tablessymbol
— 与 Python 解析树一起使用的常量token
— 与Python解析树一起使用的常量keyword
— 检验Python关键字tokenize
— 对 Python 代码使用的标记解析器tabnanny
— 模糊缩进检测pyclbr
— Python module browser supportpy_compile
— Compile Python source filescompileall
— Byte-compile Python librariesdis
— Python 字节码反汇编器pickletools
— pickle 开发者工具集formatter
— 通用格式化输出msilib
— Read and write Microsoft Installer filesmsvcrt
— Useful routines from the MS VC++ runtimewinreg
— Windows 注册表访问winsound
— Sound-playing interface for Windowsposix
— 最常见的 POSIX 系统调用pwd
— 用户密码数据库spwd
— The shadow password databasegrp
— The group databasecrypt
— Function to check Unix passwordstermios
— POSIX 风格的 tty 控制tty
— 终端控制功能pty
— 伪终端工具fcntl
— The fcntl
and ioctl
system callspipes
— 终端管道接口resource
— Resource usage informationnis
— Sun 的 NIS (黄页) 接口optparse
— 解析器的命令行选项imp
— Access the import internals