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