使用pyenv+tox时无法安装指定版本numpy的问题求助
问题:tox安装依赖时找不到符合要求的numpy版本
环境信息
- 已安装pyenv,可用Python版本:
pyenv versions system 3.8.0 3.9.16 * 3.10.1 (set by /home/me/.pyenv/version)
- 系统默认Python版本:3.6.9
tox.ini配置
[tox] envlist = unit_tests skipsdist = True [testenv] install_command = pip install {opts} {packages} deps = -rrequirements.txt commands= py.test [testenv:unit_tests] envdir = {toxworkdir}/unit_tests deps = {[testenv]deps} setenv = PYTHONPATH=. commands = python gradient_boosting_model/train_pipeline.py pytest \ -s \ -vv \ {posargs:tests/} [testenv:train] envdir = {toxworkdir}/train deps = {[testenv]deps} setenv = PYTHONPATH=. commands = python gradient_boosting_model/train_pipeline.py
requirements.txt内容
# ML requirements numpy>=1.20.0,<1.21.0 pandas>=1.3.5,<1.4.0 scikit-learn>=1.0.2,<1.1.0 feature-engine>=1.0.2,<1.1.0 joblib>=1.0.1,<1.1.0 # config parsing strictyaml>=1.3.2,<1.4.0 ruamel.yaml==0.16.12 pydantic>=1.8.1,<1.9.0 # validation marshmallow>=3.2.2,<4.0 # packaging setuptools>=41.4.0,<42.0.0 wheel>=0.33.6,<0.34.0 # testing requirements pytest>=5.3.2,<6.0.0
运行tox时的错误
tox unit_tests create: /media/me/cbe421fe-1303-4821-9392-a849bfdd00e2/MyStudy/Udemy/testing-and-monitoring/testing-and-monitoring-ml-deployments/packages/gradient_boosting_model/.tox/unit_tests unit_tests installdeps: -rrequirements.txt ERROR: invocation failed (exit code 1), logfile: /media/me/cbe421fe-1303-4821-9392-a849bfdd00e2/MyStudy/Udemy/testing-and-monitoring/testing-and-monitoring-ml-deployments/packages/gradient_boosting_model/.tox/unit_tests/log/unit_tests-1.log ==================================================================== log start ==================================================================== ERROR: Could not find a version that satisfies the requirement numpy<1.21.0,>=1.20.0 (from versions: 1.3.0, 1.4.1, 1.5.0, 1.5.1, 1.6.0, 1.6.1, 1.6.2, 1.7.0, 1.7.1, 1.7.2, 1.8.0, 1.8.1, 1.8.2, 1.9.0, 1.9.1, 1.9.2, 1.9.3, 1.10.0.post2, 1.10.1, 1.10.2, 1.10.4, 1.11.0, 1.11.1, 1.11.2, 1.11.3, 1.12.0, 1.12.1, 1.13.0, 1.13.1, 1.13.3, 1.14.0, 1.14.1, 1.14.2, 1.14.3, 1.14.4, 1.14.5, 1.14.6, 1.15.0, 1.15.1, 1.15.2, 1.15.3, 1.15.4, 1.16.0, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5, 1.16.6, 1.17.0, 1.17.1, 1.17.2, 1.17.3, 1.17.4, 1.17.5, 1.18.0, 1.18.1, 1.18.2, 1.18.3, 1.18.4, 1.18.5, 1.19.0, 1.19.1, 1.19.2, 1.19.3, 1.19.4, 1.19.5) ERROR: No matching distribution found for numpy<1.21.0,>=1.20.0 ===================================================================== log end ===================================================================== ERROR: could not install deps [-rrequirements.txt]; v = InvocationError('/media/me/cbe421fe-1303-4821-9392-a849bfdd00e2/MyStudy/Udemy/testing-and-monitoring/testing-and-monitoring-ml-deployments/packages/gradient_boosting_model/.tox/unit_tests/bin/pip install -rrequirements.txt', 1) _____________________________________________________________________ summary _____________________________________________________________________ ERROR: unit_tests: could not install deps [-rrequirements.txt]; v = InvocationError('/media/me/cbe421fe-1303-4821-9392-a849bfdd00e2/MyStudy/Udemy/testing-and-monitoring/testing-and-monitoring-ml-deployments/packages/gradient_boosting_model/.tox/unit_tests/bin/pip install -rrequirements.txt', 1)
解决方案
核心原因
你用pyenv切换了Python版本,但tox默认可能仍调用系统旧版本的pip,或者没有正确关联pyenv管理的Python环境。旧版pip无法检索到numpy 1.20+的版本(该版本支持Python3.8+,你的环境是符合要求的)。
具体解决步骤
- 强制tox使用pyenv管理的Python版本
修改tox.ini,给testenv指定明确的Python版本:
[tox] envlist = unit_tests skipsdist = True [testenv] basepython = python3.10 # 指定你想用的pyenv版本 install_command = pip install --upgrade pip {opts} {packages} # 先升级pip deps = -rrequirements.txt # 其余配置保持不变
- 手动验证pip可用性
先激活pyenv的目标版本,测试能否安装指定numpy:
pyenv activate 3.10.1 pip install --upgrade pip pip install numpy>=1.20.0,<1.21.0
如果这个命令成功,说明tox之前没正确调用该环境,调整basepython配置即可解决。
- 清理tox缓存
运行以下命令清除旧的缓存环境,避免残留问题:
tox -r
之后重新运行tox。
- 可选:放宽版本限制
如果以上步骤仍有问题,可以暂时放宽numpy的版本限制,将requirements.txt中的numpy行改为:
numpy>=1.20.0
内容的提问来源于stack exchange,提问作者KansaiRobot
相关产品推荐
相关产品推荐

