You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在tox安装依赖时设置超时时间?

问题描述

我有一个GitLab CI流水线,先完成环境配置(安装Python及其他包),然后运行tox执行测试。

GitLab CI流水线配置

stages:
  - check

before_script:
    # here you can run any commands before the pipelines start
    - apt-get -qq update && apt-get -qq install -y python3.9
    - apt-get install -y libpq-dev &&  apt-get install -y python3.9-dev
    - apt-get install -y build-essential && apt-get install -y gcc && apt-get install -y postgresql
    - apt-get install -y  postgresql-contrib && apt-get install -y  ffmpeg libsm6 libxext6
    - apt-get install -y git
    - pip install tox

check:
  stage: check
  image: gitlab.*****.com:****/*****
  environment: prod
  services:
    - name: docker:19.03.8-dind #20.10.7
      alias: docker
  only:
    - master
  script:
    - |
      echo "
      machine gitlab.*****.com
      login gitlab-ci-token
      password $CI_JOB_TOKEN
      " > ~/.netrc 
    - tox

tox.ini配置

[tox]
envlist =
    {python3.9}

[testenv]
passenv = *
setenv =
    AIRFLOW_HOME = /airflow_home
deps=
    pytest
    -r{toxinidir}/requirements.txt 
commands=
    pytest 

问题现象

tox构建环境并安装requirements.txt中的包时出现ReadTimeOutError。直接运行pip install -r requirements --default-timeout=200可正常执行,确认是超时导致的问题,希望保留tox并解决超时问题。

解决方案

方法1:通过环境变量设置pip超时

在tox.ini的[testenv]区块中添加PIP_DEFAULT_TIMEOUT环境变量,让tox调用的pip自动使用该超时值:

[testenv]
passenv = *
setenv =
    AIRFLOW_HOME = /airflow_home
    PIP_DEFAULT_TIMEOUT = 200
deps=
    pytest
    -r{toxinidir}/requirements.txt 
commands=
    pytest 

方法2:在deps中直接传递pip参数

如果需要针对tox的依赖安装单独配置,可以在deps列表最前面添加pip的超时参数:

[testenv]
passenv = *
setenv =
    AIRFLOW_HOME = /airflow_home
deps=
    --default-timeout=200
    pytest
    -r{toxinidir}/requirements.txt 
commands=
    pytest 

方法3:全局配置pip超时

在GitLab CI的before_script中添加pip全局配置,这样所有pip调用(包括tox内部的)都会生效:

before_script:
    # ... 原有环境配置命令 ...
    - pip install tox
    - pip config set global.default-timeout 200

内容的提问来源于stack exchange,提问作者Marco Fumagalli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 03:25:19