UserWarning:pkg_resources 作为 API 已弃用。参见 https://setuptools.pypa.io/en/latest/pkg_resources.html。pkg_resources 包最早将于 2025-11-30 移除。
UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30.
ID: python/setuptools-pkg-resources-deprecated-removed
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.8+ | active | — | — | — |
根因分析
代码(直接或通过依赖)导入了 pkg_resources,setuptools 已弃用该 API 并计划移除。常见于使用 pkg_resources.get_distribution 或 resource_filename 的旧包。
English
Code (directly or via a dependency) imports pkg_resources, which setuptools has deprecated and scheduled for removal. Common in older packages using pkg_resources.get_distribution or resource_filename.
解决方案
-
92% 成功率
from importlib.metadata import version, distribution print(version('requests')) from importlib.resources import files # replaces pkg_resources.resource_filename path = files('mypkg').joinpath('data.txt') -
80% 成功率
python -W error::DeprecationWarning -c 'import mypkg' # traceback shows importer pip install -U <offending-package> # upgrade to a release using importlib
无效尝试
常见但无效的做法:
-
80% 失败
Hides the symptom only; the import still executes and will hard-fail once pkg_resources is removed. Also masks the real migration need.
-
75% 失败
Pins you to an unsupported setuptools, breaks modern build backends, and merely defers the removal. Downstream packages may require newer setuptools.