Python 标准库¶
Python 语言参考 描述了 Python 语言的具体语法和语义,这份库参考则介绍了与 Python 一同发行的标准库。它还描述了通常包含在 Python 发行版中的一些可选组件。
Python 标准库非常庞大,所提供的组件涉及范围十分广泛,正如以下内容目录所显示的。这个库包含了多个内置模块 (以 C 编写),Python 程序员必须依靠它们来实现系统级功能,例如文件 I/O,此外还有大量以 Python 编写的模块,提供了日常编程中许多问题的标准解决方案。其中有些模块经过专门设计,通过将特定平台功能抽象化为平台中立的 API 来鼓励和加强 Python 程序的可移植性。
Windows 版本的 Python 安装程序通常包含整个标准库,往往还包含许多额外组件。对于类 Unix 操作系统,Python 通常会分成一系列的软件包,因此可能能要使用操作系统所提供的包管理工具来获取部分或全部可选组件。
在这个标准库以外还存在成千上万并且不断增加的其他组件 (从单独的程序、模块、软件包直到完整的应用开发框架),访问 Python 包索引 即可获取这些第三方包。
- 概述
- 内置函数
- Built-in Constants
- Built-in Types
- Truth Value Testing
- Boolean Operations ---
and
,or
,not
- 比较
- Numeric Types ---
int
,float
,complex
- Iterator Types
- Sequence Types ---
list
,tuple
,range
- Text Sequence Type ---
str
- Binary Sequence Types ---
bytes
,bytearray
,memoryview
- Set Types ---
set
,frozenset
- Mapping Types ---
dict
- Context Manager Types
- Other Built-in Types
- Special Attributes
- Built-in Exceptions
- Text Processing Services
- Binary Data Services
- 数据类型
datetime
--- Basic date and time typescalendar
--- General calendar-related functionscollections
— 容器数据类型collections.abc
--- 容器的抽象基类heapq
--- 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 enumerations
- Numeric and Mathematical Modules
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 functionsNormalDist
objects
- 函数式编程模块
- File and Directory Access
pathlib
--- Object-oriented filesystem pathsos.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
- Data Persistence
- 数据压缩和存档
- File Formats
- Cryptographic Services
- 通用操作系统服务
os
--- 操作系统接口模块io
--- Core tools for working with streamstime
--- 时间的访问和转换argparse
--- 命令行选项、参数和子命令解析器getopt
--- C-style parser for command line optionslogging
--- Logging facility for Pythonlogging.config
--- 日志记录配置logging.handlers
--- Logging handlersgetpass
--- Portable password inputcurses
--- Terminal handling for character-cell displayscurses.textpad
--- Text input widget for curses programscurses.ascii
--- Utilities for ASCII characterscurses.panel
--- A panel stack extension for cursesplatform
--- Access to underlying platform's identifying dataerrno
--- Standard errno system symbolsctypes
--- Python 的外部函数库
- Concurrent Execution
threading
--- 基于线程的并发multiprocessing
--- Process-based parallelismmultiprocessing.shared_memory
--- Provides shared memory for direct access across processes- The
concurrent
package concurrent.futures
--- Launching parallel taskssubprocess
--- Subprocess managementsched
--- Event schedulerqueue
--- A synchronized queue class_thread
—— 底层多线程 API_dummy_thread
——_thread
的替代模块dummy_threading
-- 可直接替代threading
模块。
contextvars
--- Context Variables- Networking and Interprocess Communication
asyncio
--- 异步 I/Osocket
--- Low-level networking interfacessl
--- TLS/SSL wrapper for socket objectsselect
--- Waiting for I/O completionselectors
--- 高级 I/O 复用库asyncore
--- Asynchronous socket handlerasynchat
--- Asynchronous socket command/response handlersignal
--- Set handlers for asynchronous eventsmmap
--- Memory-mapped file support
- Internet Data Handling
email
--- An email and MIME handling packagejson
--- JSON encoder and decodermailcap
--- Mailcap file handlingmailbox
--- Manipulate mailboxes in various formatsmimetypes
--- Map filenames to MIME typesbase64
--- Base16, Base32, Base64, Base85 数据编码binhex
--- Encode and decode binhex4 filesbinascii
--- Convert between binary and ASCIIquopri
--- Encode and decode MIME quoted-printable datauu
--- Encode and decode uuencode files
- Structured Markup Processing Tools
html
--- HyperText Markup Language supporthtml.parser
--- Simple HTML and XHTML parserhtml.entities
--- Definitions of HTML general entities- XML Processing Modules
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
- Internet Protocols and Support
webbrowser
--- Convenient Web-browser controllercgi
--- Common Gateway Interface supportcgitb
--- Traceback manager for CGI scriptswsgiref
--- WSGI Utilities and Reference Implementationurllib
--- URL 处理模块urllib.request
--- 用于打开 URL 的可扩展库urllib.response
--- Response classes used by urlliburllib.parse
--- Parse URLs into componentsurllib.error
--- Exception classes raised by urllib.requesturllib.robotparser
--- Parser for robots.txthttp
--- HTTP 模块- http.client --- HTTP协议客户端
ftplib
--- FTP protocol clientpoplib
--- POP3 protocol clientimaplib
--- IMAP4 protocol clientnntplib
--- NNTP protocol clientsmtplib
--- SMTP protocol clientsmtpd
--- SMTP Servertelnetlib
--- Telnet 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 server and client modulesxmlrpc.client
--- XML-RPC client accessxmlrpc.server
--- Basic XML-RPC serversipaddress
--- IPv4/IPv6 manipulation library
- Multimedia Services
audioop
--- Manipulate raw audio dataaifc
--- Read and write AIFF and AIFC filessunau
--- 读写 Sun AU 文件wave
--- 读写WAV格式文件chunk
--- Read IFF chunked datacolorsys
--- Conversions between color systemsimghdr
--- Determine the type of an imagesndhdr
--- Determine type of sound fileossaudiodev
--- Access to OSS-compatible audio devices
- Internationalization
- Program Frameworks
- Tk图形用户界面(GUI)
- 开发工具
typing
--- 类型标注支持pydoc
--- Documentation generator and online help systemdoctest
--- Test interactive Python examplesunittest
--- 单元测试框架unittest.mock
--- mock object libraryunittest.mock
--- getting started- 2to3 - 自动将 Python 2 代码转为 Python 3 代码
test
--- Regression tests package for Pythontest.support
--- Utilities for the Python test suitetest.support.script_helper
--- Utilities for the Python execution tests
- Debugging and Profiling
- Software Packaging and Distribution
- Python运行时服务
sys
--- System-specific parameters and functionssysconfig
--- Provide access to Python's configuration informationbuiltins
--- 内建对象__main__
—— 顶层脚本环境warnings
--- Warning controldataclasses
--- Data Classescontextlib
--- Utilities forwith
-statement contextsabc
—— 抽象基类atexit
--- 退出处理器traceback
--- Print or retrieve a stack traceback__future__
—— Future 语句定义gc
--- Garbage Collector interfaceinspect
--- Inspect live objectssite
--- Site-specific configuration hook
- Custom Python Interpreters
- Importing Modules
- Python Language Services
parser
--- Access Python parse treesast
--- 抽象语法树symtable
--- Access to the compiler's symbol tablessymbol
--- Constants used with Python parse treestoken
--- Constants used with Python parse treeskeyword
--- Testing for Python keywordstokenize
--- Tokenizer for Python sourcetabnanny
--- Detection of ambiguous indentationpyclbr
--- Python class browser supportpy_compile
--- Compile Python source filescompileall
--- Byte-compile Python librariesdis
--- Python 字节码反汇编器pickletools
--- Tools for pickle developers
- Miscellaneous Services
- Windows系统相关模块
- Unix 专有服务
posix
--- The most common POSIX system callspwd
--- The password databasespwd
--- The shadow password databasegrp
--- The group databasecrypt
--- Function to check Unix passwordstermios
--- POSIX style tty controltty
--- Terminal control functionspty
--- Pseudo-terminal utilitiesfcntl
--- Thefcntl
andioctl
system callspipes
--- Interface to shell pipelinesresource
--- Resource usage informationnis
--- Interface to Sun's NIS (Yellow Pages)syslog
--- Unix syslog library routines
- Superseded Modules
- Undocumented Modules