bigywater.tech
新版本特性 · 5

ModuleNotFoundError: No module named 'sre_constants':升级 Python 3.15 会撞上的 12 个移除项,以及怎么在 3.14 上提前发现

“ModuleNotFoundError: No module named ‘sre_constants’” 是项目升到 Python 3.15 后最先撞上的错之一。这篇在本机用 3.14.9 和 3.15.0 对比标准库的公开名字,把被移除的模块、函数、搬走的名字逐个实测,并演示库代码里的 DeprecationWarning 默认看不到、怎么在 3.14 上提前发现。

12 个
本篇逐个验证的在 3.15 里消失的名字:3 个模块、5 个函数、4 个 pathlib 名字
默认看不到
库代码里的 DeprecationWarning 在 3.14 默认不显示,要加 -W default 才看到
3.17 / 3.20
typing.ByteString 和 tarfile.version 在 3.15 里开始警告,计划分别在 3.17、3.20 删除

模块没了

实测 · 本机执行输出
  sre_compile
      3.14 -> ok + DeprecationWarning: module 'sre_compile' is deprecated
      3.15 -> ModuleNotFoundError: No module named 'sre_compile'
  sre_constants
      3.14 -> ok + DeprecationWarning: module 'sre_constants' is deprecated
      3.15 -> ModuleNotFoundError: No module named 'sre_constants'
  sre_parse
      3.14 -> ok + DeprecationWarning: module 'sre_parse' is deprecated
      3.15 -> ModuleNotFoundError: No module named 'sre_parse'
  3.15 里 import re._parser -> ok(没有警告)
  3.15 里 import re._constants -> ok(没有警告)
  3.15 里 import re._compiler -> ok(没有警告)

sre_compile、sre_constants、sre_parse 在 3.14 里有警告,在 3.15 里是 ModuleNotFoundError;内容在 re 的私有模块里。

函数没了

实测 · 本机执行输出
  glob:glob0
      3.14 -> ok(没有警告)
      3.15 -> AttributeError: module 'glob' has no attribute 'glob0'
  glob:glob1
      3.14 -> ok(没有警告)
      3.15 -> AttributeError: module 'glob' has no attribute 'glob1'
  platform:java_ver
      3.14 -> ok(没有警告)
      3.15 -> AttributeError: module 'platform' has no attribute 'java_ver'
  typing:no_type_check_decorator
      3.14 -> ok(没有警告)
      3.15 -> AttributeError: module 'typing' has no attribute 'no_type_check_decorator'
  ctypes:SetPointerType
      3.14 -> ok(没有警告)
      3.15 -> AttributeError: module 'ctypes' has no attribute 'SetPointerType'

glob.glob0、glob.glob1、platform.java_ver、typing.no_type_check_decorator、ctypes.SetPointerType 在 3.15 里是 AttributeError;3.14 里取这些名字没有警告。

搬走了

实测 · 本机执行输出
  pathlib:PathInfo
      3.14 -> ok(没有警告)
      3.15 -> AttributeError: module 'pathlib' has no attribute 'PathInfo'
  pathlib:DirEntryInfo
      3.14 -> ok(没有警告)
      3.15 -> AttributeError: module 'pathlib' has no attribute 'DirEntryInfo'
  pathlib:copy_info
      3.14 -> ok(没有警告)
      3.15 -> AttributeError: module 'pathlib' has no attribute 'copy_info'
  pathlib:magic_open
      3.14 -> ok(没有警告)
      3.15 -> AttributeError: module 'pathlib' has no attribute 'magic_open'
  pathlib.types:PathInfo
      3.14 -> ok(没有警告)
      3.15 -> ok(没有警告)

pathlib 上的 PathInfo、DirEntryInfo、copy_info、magic_open 在 3.15 里消失;PathInfo 在 pathlib.types 里。

开始警告

实测 · 本机执行输出
  typing:ByteString
      3.14 -> ok(没有警告)
      3.15 -> ok + DeprecationWarning: 'typing.ByteString' and 'collections.abc.ByteString' are deprecated and slated for removal in Python 3.17
  tarfile:version
      3.14 -> ok(没有警告)
      3.15 -> ok + DeprecationWarning: 'version' is deprecated and slated for removal in Python 3.20
  Python 3.15(不显示警告)(退出码 0):
      'ByteString' in dir(typing) = False | hasattr(typing, "ByteString") = True
      'version' in dir(tarfile) = False | hasattr(tarfile, "version") = True

typing.ByteString、tarfile.version 在 3.15 里警告但仍能取到,dir() 里已经看不到。

提前发现

实测 · 本机执行输出
  3.14 默认(退出码 0):
      main: 导入 mylib 成功,f() -> []
  3.14 -W default(退出码 0):
      main: 导入 mylib 成功,f() -> []
      <tmp>/proj/mylib.py:2: DeprecationWarning: module 'sre_parse' is deprecated
      <tmp>/proj/mylib.py:5: DeprecationWarning: glob.glob0 is deprecated and will be removed in Python 3.15. Use glob.glob and pass a directory to its root
  3.14 -W error::DeprecationWarning(退出码 1):
      Traceback (most recent call last):
        File "<tmp>/proj/main.py", line 2, in <module>
        File "<tmp>/proj/mylib.py", line 2, in <module>
        File "<py3.14>/lib/python3.14/sre_parse.py", line 2, in <module>
          warnings.warn(f"module {__name__!r} is deprecated",
                        DeprecationWarning,
                        stacklevel=2)
      DeprecationWarning: module 'sre_parse' is deprecated
  3.15 默认(退出码 1):
      Traceback (most recent call last):
        File "<tmp>/proj/main.py", line 2, in <module>
        File "<tmp>/proj/mylib.py", line 2, in <module>
      ModuleNotFoundError: No module named 'sre_parse'. Did you mean: 'argparse'?

库代码里的 DeprecationWarning 默认不显示;在 3.14 上加 -W default 或 -W error::DeprecationWarning 跑测试。

分步演示

移除项清单
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64 / Homebrew 的 Python 3.14.9 与 3.15.0(同一台机器上两个解释器并排跑同一份代码;两者都不是自由线程构建,也都没有启用 JIT);每个例子是临时目录里的独立小脚本,用子进程运行并取真实的 stdout / stderr(路径换成 );计时用 time.perf_counter,数字每次运行会有小幅抖动。被移除的名字是先对比两个版本所有标准库模块的 dir() 得到候选,再用 getattr / import 在子进程里逐个验证(dir() 里消失不等于取不到,见第四步);警告文本取自 warnings.catch_warnings 捕获的真实内容
    • 没有验证:候选名字只来自对 dir() 的对比,没有逐条核对发行说明,可能漏掉不在 dir() 里的行为变化(参数的改动、返回值的改动);第三方库(setuptools、pyparsing、老版本的框架等)有多少还在用这些被移除的名字;pathlib 另外三个名字的新位置;替代写法 re._parser 等私有模块的稳定性;PyPy 等其他实现;Windows 和 Linux 上被移除名字的差异(例如 platform.java_ver 的实际用途)
    京ICP备2026067502号-1京公网安备11010502063487号