弃用

计划在 Python 3.15 中移除

  • 导入系统:

    • 当设置 __spec__.cached 失败时在模块上设置 __cached__ 的做法已被弃用。 在 Python 3.15 中,__cached__ 将不会再被导入系统或标准库设置或纳入其考量。 (gh-97879)

    • 当设置 __spec__.parent 失败时在模块上设置 __package__ 的做法已被弃用。在 Python 3.15 中,__package__ 将不会再被导入系统或标准库纳入考虑。 (gh-97879)

  • ctypes:

    • 未写入文档的 ctypes.SetPointerType() 函数自 Python 3.13 起已被弃用。

  • http.server:

    • 过时且很少被使用的 CGIHTTPRequestHandler 自 Python 3.13 起已被弃用。 不存在直接的替代品。 对于建立带有请求处理器的 Web 服务器接口 任何东西 都比 CGI 强。

    • 用于 python -m http.server 命令行界面的 --cgi 旗标自 Python 3.13 起已被弃用。

  • importlib:

    • load_module() 方法:改用 exec_module()

  • pathlib:

    • .PurePath.is_reserved() 自 Python 3.13 起已被弃用。 请使用 os.path.isreserved() 来检测 Windows 上的保留路径。

  • platform:

    • platform.java_ver() 自 Python 3.13 起已被弃用。 此函数仅对 Jython 支持有用,具有令人困惑的 API,并且大部分未经测试。

  • sysconfig:

  • threading:

    • RLock() will take no arguments in Python 3.15. Passing any arguments has been deprecated since Python 3.14, as the Python version does not permit any arguments, but the C version allows any number of positional or keyword arguments, ignoring every argument.

  • types:

    • types.CodeType: 访问 codeobject.co_lnotab 的做法自 3.10 起已根据 PEP 626 被弃用并曾计划在 3.12 中移除,但在 3.12 中仅设置了 DeprecationWarning。 可能会在 3.15 中移除。 (由 Nikita Sobolev 在 gh-101866 中贡献。)

  • typing:

    • 未写入文档的用于创建 NamedTuple 类的关键字参数语法 (例如 Point = NamedTuple("Point", x=int, y=int)) 自 Python 3.13 起已被弃用。 请改用基于类的语法或函数语法。

    • 当使用 TypedDict 的函数式语法时,不向 fields 形参传递值 (TD = TypedDict("TD")) 或传递 None (TD = TypedDict("TD", None)) 的做法自 Python 3.13 起已被弃用。请改用 class TD(TypedDict): passTD = TypedDict("TD", {}) 来创建一个零字段的 TypedDict。

    • typing.no_type_check_decorator() 装饰器自 Python 3.13 起已被弃用。 存在于 typing 模块八年之后,它仍未被任何主要类型检查器所支持。

  • sre_compile, sre_constantssre_parse 模块。

  • wave:

    • Wave_readWave_write 类的 getmark(), setmark()getmarkers() 方法自 Python 3.13 起已被弃用。

  • zipimport:

    • zipimport.zipimporter.load_module() 自 Python 3.10 起已被弃用。 请改用 exec_module()。 (gh-125746。)

计划在 Python 3.16 中移除

计划在 Python 3.17 中移除

  • datetime:

    • 使用不带年份的包含 %e (月份日期序号) 的格式字符串的 strptime() 调用自 Python 3.15 起已被弃用。 (由 Stan Ulbrych 在 gh-70647 中贡献。)

  • collections.abc:

    • collections.abc.ByteString 计划在 Python 3.17 中移除。

      使用 isinstance(obj, collections.abc.Buffer) 来测试 obj 是否在运行时实现了 缓冲区协议。要用于类型标注,则使用 Buffer 或是显式指明你的代码所支持类型的并集 (例如 bytes | bytearray | memoryview)。

      ByteString 原本是想作为 bytesbytearray 的超类型的抽象基类提供。 不过,由于该 ABC 从未有任何方法,知道一个对象是 ByteString 的实例并不能真正告诉你有关该对象的任何有用信息。其他常见缓冲区类型如 memoryview 同样不能被当作是 ByteString 的子类型(无论是在运行时还是对于静态类型检查器)。

      请参阅 PEP 688 了解详情。 (由 Shantanu Jain 在 gh-91896 中贡献。)

  • encodings:

  • webbrowser:

    • webbrowser.MacOSXOSAScript 已被弃用并改用 webbrowser.MacOS。 (gh-137586)

  • typing:

    • 在 Python 3.14 之前,旧式的联合是通过私有类 typing._UnionGenericAlias 实现的。实现已不再需要该类,但为向后兼容性保留了该类,并计划在 Python 3.17 中删除。用户应使用记录在案的内省助手函数,如 typing.get_origin()typing.get_args(),而不是依赖于私有的实现细节。

    • typing.ByteString 自 Python 3.9 起已被弃用,计划在 Python 3.17 中移除。

      使用 isinstance(obj, collections.abc.Buffer) 来测试 obj 是否在运行时实现了 缓冲区协议。要用于类型标注,则使用 Buffer 或是显式指明你的代码所支持类型的并集 (例如 bytes | bytearray | memoryview)。

      ByteString 原本是想作为 bytesbytearray 的超类型的抽象基类提供。 不过,由于该 ABC 从未有任何方法,知道一个对象是 ByteString 的实例并不能真正告诉你有关该对象的任何有用信息。其他常见缓冲区类型如 memoryview 同样不能被当作是 ByteString 的子类型(无论是在运行时还是对于静态类型检查器)。

      请参阅 PEP 688 了解详情。 (由 Shantanu Jain 在 gh-91896 中贡献。)

计划在 Python 3.18 中移除

  • 当需要一个文件描述符时将不再接受布尔值。 (由 Serhiy Storchaka 在 gh-82626 中贡献。)

  • decimal:

    • 非标准且未写入文档的 Decimal 格式说明符 'N',它仅在 decimal 模块的 C 实现中受到支持,自 Python 3.13 起已被弃用。 (由 Serhiy Storchaka 在 gh-89902 中贡献。)

  • PEP 829 定义的弃用:

    • name.pth 文件中的 import 行会被静默地忽略。

    (由 Barry Warsaw 在 gh-148641 中贡献。)

计划在 Python 3.19 中移除

  • ctypes:

    • 在非 Windows 平台上,通过设置 _pack_ 而非 _layout_,隐式切换到与 MSVC 兼容的结构布局。

  • hashlib:

    • 在哈希函数构造器如 new() 或者直接使用哈希算法名称的构造器如 md5()sha256() 中,它们可选的初始数据形参在不同的 hashlib 实现中也可使用名为 data=string= 的关键字参数形式传入。

      string 关键字参数名称的支持现已被弃用并计划在 Python 3.19 中移除。

      在 Python 3.13 之前,string 关键字形参没有根据哈希函数的后端实现得到正确的支持。 建议将初始数据作为位置参数传入以获得最大的向下兼容性。

  • http.cookies:

  • imaplib:

    • Altering IMAP4.file is now deprecated and slated for removal in Python 3.19. This property is now unused and changing its value does not automatically close the current file.

      Before Python 3.14, this property was used to implement the corresponding read() and readline() methods for IMAP4 but this is no longer the case since then.

Pending removal in Python 3.20

计划在未来版本中移除

以下 API 将会被移除,尽管具体时间还未确定。

  • argparse:

    • 嵌套参数组和嵌套互斥组已被弃用。

    • 将未写入文档的关键字参数 prefix_chars 传递给 add_argument_group() 的做法现在已被弃用。

    • argparse.FileType 类型转换器已弃用。

  • builtins:

    • 生成器: throw(type, exc, tb)athrow(type, exc, tb) 签名已被弃用:请改用 throw(exc)athrow(exc),即单参数签名。

    • 目前 Python 接受数字类字面值后面紧跟关键字的写法,例如 0in x, 1or x, 0if 1else 2。它允许像 [0x1for x in y] 这样令人困惑且有歧义的表达式 (它可以被解读为 [0x1 for x in y] 或者 [0x1f or x in y])。如果数字类字面值后面紧跟关键字 and, else, for, if, in, isor 中的一个将会引发语法警告。在未来的版本中它将改为语法错误。 (gh-87999)

    • __index__()__int__() 方法返回非 int 类型的支持:将要求这些方法必须返回 int 的子类的实例。

    • __float__() 方法返回 float 的子类的支持:将要求这些方法必须返回 float 的实例。

    • __complex__() 方法返回 complex 的子类的支持:将要求这些方法必须返回 complex 的实例。

    • 传入一个复数作为 complex() 构造器中的 realimag 参数的做法现在已被弃用;它应当仅作为单个位置参数被传入。(由 Serhiy Storchaka 在 gh-109218 中贡献。)

  • calendar: calendar.Januarycalendar.February 常量已被弃用并由 calendar.JANUARYcalendar.FEBRUARY 替代。 (由 Prince Roshan 在 gh-103636 中贡献。)

  • codecs: 请使用 open() 代替 codecs.open()。 (gh-133038)

  • codeobject.co_lnotab: 改用 codeobject.co_lines() 方法。

  • datetime:

    • utcnow(): 使用 datetime.datetime.now(tz=datetime.UTC)

    • utcfromtimestamp(): 使用 datetime.datetime.fromtimestamp(timestamp, tz=datetime.UTC)

  • gettext: 复数值必须是一个整数。

  • importlib:

  • importlib.metadata:

    • EntryPoints 元组接口。

    • 返回值中隐式的 None

  • logging: warn() 方法自 Python 3.3 起已被弃用,请改用 warning().

  • mailbox: 对 StringIO 输入和文本模式的使用已被弃用,改用 BytesIO 和二进制模式。

  • os: 在多线程的进程中调用 os.register_at_fork()

  • os.path: os.path.commonprefix() is deprecated, use os.path.commonpath() for path prefixes. The os.path.commonprefix() function is being deprecated due to having a misleading name and module. The function is not safe to use for path prefixes despite being included in a module about path manipulation, meaning it is easy to accidentally introduce path traversal vulnerabilities into Python programs by using this function.

  • pydoc.ErrorDuringImport: 使用元组值作为 exc_info 形参的做法已被弃用,应使用异常实例。

  • re: 现在对于正则表达式中的数字分组引用和分组名称将应用更严格的规则。现在只接受 ASCII 数字序列作为数字引用。字节串模式和替换字符串中的分组名称现在只能包含 ASCII 字母和数字以及下划线。 (由 Serhiy Storchaka 在 gh-91760 中贡献。)

  • shutil: rmtree()onerror 形参在 Python 3.12 中已被弃用;请改用 onexc 形参。

  • ssl 选项和协议:

    • ssl.SSLContext 不带 protocol 参数的做法已被弃用。

    • ssl.SSLContext: set_npn_protocols()selected_npn_protocol() 已被弃用:请改用 ALPN。

    • ssl.OP_NO_SSL* 选项

    • ssl.OP_NO_TLS* 选项

    • ssl.PROTOCOL_SSLv3

    • ssl.PROTOCOL_TLS

    • ssl.PROTOCOL_TLSv1

    • ssl.PROTOCOL_TLSv1_1

    • ssl.PROTOCOL_TLSv1_2

    • ssl.TLSVersion.SSLv3

    • ssl.TLSVersion.TLSv1

    • ssl.TLSVersion.TLSv1_1

  • threading 的方法:

  • typing.Text (gh-92332).

  • 内部类 typing._UnionGenericAlias 不再用于实现 typing.Union。为了保持使用该私有类的用户的兼容性,将至少在 Python 3.17 之前提供兼容性垫片。 (由 Jelle Zijlstra 在 gh-105499 中贡献。)

  • unittest.IsolatedAsyncioTestCase: 从测试用例返回不为 None 的值的做法已被弃用。

  • urllib.parse 函数已被弃用:改用 urlparse()

    • splitattr()

    • splithost()

    • splitnport()

    • splitpasswd()

    • splitport()

    • splitquery()

    • splittag()

    • splittype()

    • splituser()

    • splitvalue()

    • to_bytes()

  • wsgiref: SimpleHandler.stdout.write() 不应执行部分写入。

  • xml.etree.ElementTree: 对 Element 的真值测试已被弃用。在未来的发布版中它将始终返回 True。建议改用显式的 len(elem)elem is not None 测试。

  • sys._clear_type_cache() 已弃用,请改用 sys._clear_internal_caches()

软弃用

尚无计划移除 soft deprecated API。

  • re.match() and re.Pattern.match() are now soft deprecated in favor of the new re.prefixmatch() and re.Pattern.prefixmatch() APIs, which have been added as alternate, more explicit names. These are intended to be used to alleviate confusion around what match means by following the Zen of Python's "Explicit is better than implicit" mantra. Most other language regular expression libraries use an API named match to mean what Python has always called search.

    We do not plan to remove the older match() name, as it has been used in code for over 30 years. Code supporting older versions of Python should continue to use match(), while new code should prefer prefixmatch(). See prefixmatch() vs. match().

    (由 Gregory P. Smith 在 gh-86519 以及 Hugo van Kemenade 在 gh-148100 中贡献。)

C API 的弃用项

计划在 Python 3.15 中移除

计划在 Python 3.16 中移除

  • 捆绑的 libmpdec 副本。

计划在 Python 3.18 中移除

计划在 Python 3.19 中移除

  • PEP 456 对于字符串哈希方案定义的嵌入方支持。

Pending removal in Python 3.20

计划在未来版本中移除

以下 API 已被弃用,将被移除,但目前尚未确定移除日期。