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

Azure Pipeline容器中Pip安装超时及Pytest未找到问题求助

问题描述

Azure DevOps Pipeline配置

stages:
- stage: unit_tests
  displayName: Run Tests
  jobs:
  - job: create_image
    displayName: Create image
    pool:
      name: SomePool
    steps:
    - script: |
        docker build -t someImage:latest -f Dockerfile .
        docker push repo/someImage:latest
      displayName: Create Base Image
  - job:
    displayName: Unit tests
    dependsOn: create_image
    pool: 
      name: SomePool
    container:
      image: someImage:latest
      endpoint: someServiceConnection
    steps:
      - script: |
          python -m pip install --upgrade pip
          python -m pip install --user pytest pytest-cov
          <some other commands>
        displayName: Install Dependencies
      - script: |
          python -m pytest -c pytest.ini tests/
        displayName: Run pytest

初始报错(pip安装超时)

WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f7eb95ff2b0>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/pip/
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f34a25d4670>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/pytest/

尝试操作后的问题

将pip安装命令移至Dockerfile:

RUN pip install -r requirements.txt
 RUN python -m pip install --upgrade pip
 RUN python -m pip install --user pytest

但Pipeline运行时提示pytest未找到,执行pip list确认pytest未安装:

/usr/local/bin/python: No module named pytest
##[error]Bash exited with code '1'.

注:在同一池机器上手动执行操作可得到正常结果。


解决思路

一、解决pip安装超时问题

  • 配置代理:容器内可能未继承池机器的代理配置,需手动添加。可在Dockerfile中设置环境变量:
    ENV http_proxy=http://your-proxy-address:port
    ENV https_proxy=http://your-proxy-address:port
    
    或在pip命令中直接指定代理:
    python -m pip install --upgrade pip --proxy http://your-proxy-address:port
    
  • 更换PyPI镜像源:使用国内镜像源提升下载速度,避免超时。比如在pip安装时添加-i参数:
    python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
    python -m pip install pytest pytest-cov -i https://pypi.tuna.tsinghua.edu.cn/simple
    
    也可在Dockerfile中配置全局镜像源:
    RUN mkdir -p ~/.pip
    RUN echo "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple" > ~/.pip/pip.conf
    
  • 延长超时时间:通过--default-timeout参数增加pip的连接超时时间:
    python -m pip install --upgrade pip --default-timeout=60
    

二、解决pytest未被识别的问题

  • 移除--user参数:--user会将包安装到当前用户的私有目录,容器运行时可能使用不同用户,导致包不在系统PATH中。直接全局安装:
    RUN python -m pip install --upgrade pip
    RUN pip install -r requirements.txt
    RUN pip install pytest pytest-cov
    
  • 验证Dockerfile执行顺序:先升级pip,再安装依赖和pytest,避免旧版本pip导致安装失败。
  • 强制重新构建镜像:Docker默认会使用缓存,可能导致修改后的Dockerfile未生效。在构建命令中添加--no-cache参数:
    docker build --no-cache -t someImage:latest -f Dockerfile .
    
  • 检查容器内Python环境:在测试job中添加调试步骤,确认环境一致性:
    - script: |
        which python
        pip --version
        echo $PATH
        pip list
      displayName: Check Python Environment
    
    对比手动操作时的输出,确认是否使用了相同的Python解释器和环境变量。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 14:52:42