Python 标准库¶
Python 语言参考手册 描述了 Python 语言的具体语法和语义,这份库参考则介绍了与 Python 一同发行的标准库。它还描述了通常包含在 Python 发行版中的一些可选组件。
Python 标准库非常庞大,所提供的组件涉及范围十分广泛,正如以下内容目录所显示的。这个库包含了多个内置模块 (以 C 编写),Python 程序员必须依靠它们来实现系统级功能,例如文件 I/O,此外还有大量以 Python 编写的模块,提供了日常编程中许多问题的标准解决方案。其中有些模块经过专门设计,通过将特定平台功能抽象化为平台中立的 API 来鼓励和加强 Python 程序的可移植性。
Windows 版本的 Python 安装程序通常包含整个标准库,往往还包含许多额外组件。对于类 Unix 操作系统,Python 通常会分成一系列的软件包,因此可能需要使用操作系统所提供的包管理工具来获取部分或全部可选组件。
在标准库以外,还存在成千上万并且不断增加的其他组件集(从单独的程序和模块到软件包以及完整的应用程序开发框架),这些组件集可以从`Python 包索引 <https://pypi.org>`_ 获取。
- 概述
- 内置函数
- 内置常量
- 内置类型
- 内置异常
- 文本处理服务
string
--- Common string operationsre
--- Regular expression operationsdifflib
--- Helpers for computing deltastextwrap
--- Text wrapping and fillingunicodedata
--- Unicode Databasestringprep
--- Internet String Preparationreadline
--- GNU readline interfacerlcompleter
--- Completion function for GNU readline
- 二进制数据服务
- 数据类型
datetime
--- Basic date and time typeszoneinfo
--- IANA time zone supportcalendar
--- General calendar-related functionscollections
--- Container datatypescollections.abc
--- Abstract Base Classes for Containersheapq
--- Heap queue algorithmbisect
--- Array bisection algorithmarray
--- Efficient arrays of numeric valuesweakref
--- 弱引用types
--- Dynamic type creation and names for built-in typescopy
--- Shallow and deep copy operationspprint
--- Data pretty printerreprlib
--- Alternaterepr()
implementationenum
--- Support for enumerationsgraphlib
--- Functionality to operate with graph-like structures
- 数字和数学模块
numbers
--- Numeric abstract base classesmath
--- Mathematical functionscmath
--- Mathematical functions for complex numbersdecimal
--- Decimal fixed point and floating point arithmeticfractions
--- Rational numbersrandom
--- Generate pseudo-random numbersstatistics
--- Mathematical statistics functions
- 函数式编程模块
- 文件和目录访问
pathlib
--- 面向对象的文件系统路径os.path
--- Common pathname manipulationsfileinput
--- Iterate over lines from multiple input streamsstat
--- Interpretingstat()
resultsfilecmp
--- File and Directory Comparisonstempfile
--- Generate temporary files and directoriesglob
--- Unix style pathname pattern expansionfnmatch
--- Unix filename pattern matchinglinecache
--- Random access to text linesshutil
--- High-level file operations
- 数据持久化
- 数据压缩和存档
- 文件格式
- 加密服务
- 通用操作系统服务
os
--- 多种操作系统接口io
--- Core tools for working with streamstime
--- Time access and conversionsargparse
--- Parser for command-line options, arguments and sub-commandsgetopt
--- C-style parser for command line optionslogging
--- Logging facility for Pythonlogging.config
--- Logging configurationlogging.handlers
--- Logging handlersgetpass
--- Portable password inputcurses
--- Terminal handling for character-cell displayscurses.textpad
--- 用于 curses 程序的文本输入控件curses.ascii
--- Utilities for ASCII characterscurses.panel
--- A panel stack extension for cursesplatform
--- Access to underlying platform's identifying dataerrno
--- Standard errno system symbolsctypes
--- A foreign function library for Python
- 并发执行
threading
--- Thread-based parallelismmultiprocessing
--- Process-based parallelismmultiprocessing.shared_memory
--- Shared memory for direct access across processesconcurrent
包concurrent.futures
--- Launching parallel taskssubprocess
--- Subprocess managementsched
--- Event schedulerqueue
--- A synchronized queue classcontextvars
--- Context Variables_thread
--- Low-level threading API
- 网络和进程间通信
- 互联网数据处理
email
--- An email and MIME handling packagejson
--- JSON encoder and decodermailbox
--- Manipulate mailboxes in various formatsmimetypes
--- Map filenames to MIME typesbase64
--- Base16, Base32, Base64, Base85 Data Encodingsbinascii
--- Convert between binary and ASCIIquopri
--- Encode and decode MIME quoted-printable data
- 结构化标记处理工具
html
--- HyperText Markup Language supporthtml.parser
--- Simple HTML and XHTML parserhtml.entities
--- Definitions of HTML general entities- XML处理模块
xml.etree.ElementTree
--- The 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 Utilitiesxml.sax.xmlreader
--- Interface for XML parsersxml.parsers.expat
--- Fast XML parsing using Expat
- 互联网协议和支持
webbrowser
--- Convenient web-browser controllerwsgiref
--- WSGI Utilities and Reference Implementationurllib
--- URL handling modulesurllib.request
--- Extensible library for opening URLsurllib.response
--- urllib 使用的 Response 类urllib.parse
--- Parse URLs into componentsurllib.error
--- Exception classes raised by urllib.requesturllib.robotparser
--- Parser for robots.txthttp
--- HTTP 模块http.client
--- HTTP protocol clientftplib
--- FTP protocol clientpoplib
--- POP3 protocol clientimaplib
--- IMAP4 protocol clientsmtplib
--- SMTP protocol clientuuid
--- UUID objects according to RFC 4122socketserver
--- A framework for network servershttp.server
--- HTTP servershttp.cookies
--- HTTP state managementhttp.cookiejar
--- Cookie handling for HTTP clientsxmlrpc
--- XMLRPC 服务端与客户端模块xmlrpc.client
--- XML-RPC client accessxmlrpc.server
--- Basic XML-RPC serversipaddress
--- IPv4/IPv6 manipulation library
- 多媒体服务
- 国际化
- 程序框架
- Tk图形用户界面(GUI)
tkinter
--- Python interface to Tcl/Tktkinter.colorchooser
--- Color choosing dialogtkinter.font
--- Tkinter font wrapper- Tkinter 对话框
tkinter.messagebox
--- Tkinter message promptstkinter.scrolledtext
--- Scrolled Text Widgettkinter.dnd
--- Drag and drop supporttkinter.ttk
--- Tk themed widgetstkinter.tix
--- TK扩展包- IDLE
- 开发工具
typing
—— 对类型提示的支持pydoc
--- Documentation generator and online help system- Python 开发模式
doctest
--- Test interactive Python examplesunittest
--- Unit testing frameworkunittest.mock
--- mock object libraryunittest.mock
--- getting started- 2to3 --- 自动化的 Python 2 到 3 代码转写
test
--- Regression tests package for Pythontest.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
--- 用于导入测试的工具test.support.warnings_helper
--- 用于警告测试的工具
- 调试和分析
- 软件打包和分发
- Python 运行时服务
sys
--- System-specific parameters and functionssys.monitoring
--- Execution event monitoringsysconfig
--- Provide access to Python's configuration informationbuiltins
--- Built-in objects__main__
--- Top-level code environmentwarnings
--- Warning controldataclasses
--- 数据类contextlib
--- 为with
语句上下文提供的工具abc
--- Abstract Base Classesatexit
--- Exit handlerstraceback
--- Print or retrieve a stack traceback__future__
--- Future statement definitionsgc
--- Garbage Collector interfaceinspect
--- Inspect live objectssite
--- Site-specific configuration hook
- 自定义 Python 解释器
- 导入模块
zipimport
--- Import modules from Zip archivespkgutil
--- Package extension utilitymodulefinder
--- Find modules used by a scriptrunpy
--- Locating and executing Python modulesimportlib
---import
的实现importlib.resources
-- Package resource reading, opening and accessimportlib.resources.abc
-- Abstract base classes for resourcesimportlib.metadata
-- 访问软件包元数据sys.path
模块搜索路径的初始化
- Python 语言服务
ast
--- Abstract Syntax Treessymtable
--- Access to the compiler's symbol tablestoken
--- Constants used with Python parse treeskeyword
--- Testing for Python keywordstokenize
--- Tokenizer for Python sourcetabnanny
--- Detection of ambiguous indentationpyclbr
--- Python module browser supportpy_compile
--- Compile Python source filescompileall
--- Byte-compile Python librariesdis
--- Python 字节码反汇编器pickletools
--- Tools for pickle developers
- Windows系统相关模块
- Unix 专有服务
posix
--- The most common POSIX system callspwd
--- The password databasegrp
--- The group databasetermios
--- POSIX style tty controltty
--- Terminal control functionspty
--- Pseudo-terminal utilitiesfcntl
--- Thefcntl
andioctl
system callsresource
--- Resource usage informationsyslog
--- Unix syslog library routines
- 模块命令行界面(CLI)
- 被取代的模块
aifc
--- 读写 AIFF 和 AIFC 文件audioop
--- 处理原始音频数据cgi
--- 通用网关接口支持cgitb
--- 用于 CGI 脚本的回溯管理器chunk
--- 读取 IFF 分块数据crypt
—— 验证 Unix 口令的函数imghdr
--- 推测图像类型mailcap
--- Mailcap 文件处理msilib
--- 读写 Microsoft Installer 文件nis
--- Sun 的 NIS (黄页) 接口nntplib
--- NNTP 协议客户端optparse
--- Parser for command line optionsossaudiodev
--- 访问兼容OSS的音频设备pipes
--- 终端管道接口sndhdr
--- 推测声音文件的类型spwd
—— shadow 密码库sunau
--- 读写 Sun AU 文件telnetlib
-- Telnet 客户端uu
--- 对 uuencode 文件进行编码与解码xdrlib
--- 编码与解码 XDR 数据
- 安全考量