安装旧版本teradataml时,能否强制安装兼容的依赖包?
问题场景
在Python 3.7.4的venv虚拟环境中,执行pip install teradataml==17.0.0.3时,pip自动安装的依赖包中包含了SQLAlchemy 2.0.2,运行代码时触发以下错误:
ArgumentError: Additional keyword arguments are not accepted by this function/method. The presence of **kw is for pep-484 typing purposes
代码示例:
from teradataml import create_context class ConnectToTeradata: def __init__(self): host = 'AWESOME_HOST' username = 'johnnyMnemonic' password = 'keanu4life' self.connection = create_context(host = host, user = username, password = password) def __del__(self): print("Closing connection") self.connection.dispose() ConnectToTeradata()
经测试,若先手动安装SQLAlchemy==1.4.26再安装teradataml,可正常建立连接,说明teradataml 17.0.0.3与SQLAlchemy 2.x版本不兼容。疑问在于:能否让pip install teradataml==17.0.0.3时自动安装兼容的依赖版本,而非最新版?
可行解决方案
1. 使用约束文件限制依赖版本
创建一个名为constraints.txt的文件,写入SQLAlchemy的版本约束:
sqlalchemy<=1.4.26
执行安装命令时指定该约束文件:
pip install teradataml==17.0.0.3 --constraint constraints.txt
此方式会强制pip在满足teradataml依赖的前提下,选择不超过1.4.26版本的SQLAlchemy。
2. 安装时直接指定兼容的SQLAlchemy版本
在安装命令中同时指定teradataml和SQLAlchemy的版本:
pip install teradataml==17.0.0.3 sqlalchemy==1.4.26
pip会自动处理依赖关系,确保不会安装超出指定版本的SQLAlchemy。
补充说明
出现此问题的根源是teradataml 17.0.0.3的依赖声明未严格限制SQLAlchemy的版本上限(仅指定了最低兼容版本),导致pip默认拉取最新版本的SQLAlchemy,进而引发兼容性问题。通过上述两种方式,可强制锁定兼容的依赖版本。
内容的提问来源于stack exchange,提问作者Parlstow

