问题描述
每次运行程序时都会显示:
C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\xtquant_init_.py:8: 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. Refrain from using this package or pin to Setuptools<81.
from pkg_resources import get_distribution
开始以为是升级 3.13 的原因,恢复 3.12 还是这样。
xtquant 库已经是最新的 xtquant_250516
解决方案
您遇到的这个警告信息 UserWarning: pkg_resources is deprecated as an API 是因为 Python 的包管理工具 setuptools 在其较新的版本(>= 81.0.0)中,正式宣布弃用了 pkg_resources 模块,并推荐开发者改用 Python 标准库中的 importlib.metadata 或 importlib.resources。
由于 xtquant 库的底层代码(在 __init__.py 中)仍然使用了 from pkg_resources import get_distribution 来获取版本信息,因此当您的环境中安装了较新版本的 setuptools 时,就会触发这个警告。这与您使用的 Python 版本(3.12 或 3.13)关系不大,主要取决于 setuptools 的版本。
这个警告不会影响程序的正常运行和交易逻辑,仅仅是一个提示。如果您希望消除这个警告,可以尝试以下几种解决方法:
解决方法一:降级 setuptools 版本(推荐)
正如警告信息中提示的 pin to Setuptools<81,您可以将环境中的 setuptools 降级到 81 以下的版本。打开命令行(CMD 或 PowerShell),运行以下命令:
pip install "setuptools<81.0.0"
降级完成后,再次运行程序,警告信息就会消失。
解决方法二:在代码中忽略该警告
如果您不想修改环境中的包版本,可以在您的 Python 策略代码的最开头(在导入 xtquant 之前),使用 Python 的 warnings 模块来忽略这个特定的警告:
import warnings
# 忽略 pkg_resources 弃用警告
warnings.filterwarnings("ignore", category=UserWarning, module="xtquant")
# 或者更宽泛地忽略所有 DeprecationWarning
# warnings.filterwarnings("ignore", category=DeprecationWarning)
from xtquant import xtdata
from xtquant import xttrader
# 您的其他代码...
解决方法三:等待官方更新
由于 pkg_resources 计划在 2025 年底被彻底移除,xtquant 的开发团队在未来的版本更新中必然会修复这个问题(将 pkg_resources 替换为 importlib.metadata)。您可以保持关注迅投官方的更新动态,当发布修复此问题的新版本时,直接升级 xtquant 即可。
总结:目前最简单直接的方法是降级 setuptools 或在代码中屏蔽警告。这只是一个兼容性提示,完全不影响您的量化策略执行,请放心使用。