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

CircleCI 2.0环境变量配置异常:PATH未添加致Python版本错误

解决CircleCI 2.0中conda环境变量与Python版本不生效的问题

我一眼就看出问题所在了——CircleCI的每个run步骤都是独立的shell会话,你在第一个步骤里设置的PATH和conda环境激活状态,不会自动传递到后续的run步骤中。这就是为什么手动SSH执行有效,但自动构建时,后续步骤还是用系统默认的Python 2.7。

核心问题分析

  • CircleCI的每个run指令都会启动一个全新的bash shell,之前步骤中export的环境变量、激活的conda环境都不会被保留。
  • 你在第一个run里创建了testenv环境并激活,但这个激活状态只在当前shell会话有效,下一个run步骤会直接回到原始系统环境。
  • 报错里的print语句问题,本质是Python版本不匹配:Python 2.7不支持print("xxx", file=sys.stderr)这种写法,而你的setup.py是为Python 3.x编写的。

修复方案(两种可选)

方案1:使用conda环境的绝对路径(最可靠)

不需要依赖环境变量传递,直接用conda环境里的pip/python绝对路径执行命令,彻底避免环境继承问题:

修改后的config.yml:

version: 2
jobs:
  build:
    branches:
      only:
        - dev
    machine: true
    working_directory: ~/repo
    steps:
      - checkout
      - run:
          name: install miniconda
          command: |
            export MINICONDA=$HOME/miniconda
            export PATH="$MINICONDA/bin:$PATH"
            hash -r
            wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
            bash miniconda.sh -b -f -p $MINICONDA
            conda config --set always_yes yes
            conda update conda
            conda info -a
            conda create -n testenv python=3.6.0
      - run:
          name: install requirements
          command: |
            # 直接使用testenv环境下的pip绝对路径
            $HOME/miniconda/envs/testenv/bin/pip install -r python-requirements.txt
            $HOME/miniconda/envs/testenv/bin/pip install pytest-ordering
      - run:
          name: download sample dataset
          command: |
            cd /home/circleci/repo/unit_tests/test_data
            wget http://cf.10xgenomics.com/samples/cell-exp/2.1.0/t_3k/t_3k_filtered_gene_bc_matrices.tar.gz
            tar -xvfz t_3k_filtered_gene_bc_matrices.tar.gz
      - run:
          name: run tests
          command: |
            # 使用testenv环境下的pytest绝对路径
            $HOME/miniconda/envs/testenv/bin/pytest ./unit_tests
      - store_artifacts:
          path: test-reports
          destination: test-reports

方案2:让conda环境在所有步骤中生效(通过初始化shell)

在安装miniconda后执行conda init bash,后续步骤通过加载bash配置文件来继承conda环境,更贴近本地开发习惯:

version: 2
jobs:
  build:
    branches:
      only:
        - dev
    machine: true
    working_directory: ~/repo
    steps:
      - checkout
      - run:
          name: install miniconda
          command: |
            export MINICONDA=$HOME/miniconda
            export PATH="$MINICONDA/bin:$PATH"
            hash -r
            wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
            bash miniconda.sh -b -f -p $MINICONDA
            conda config --set always_yes yes
            conda update conda
            conda info -a
            conda create -n testenv python=3.6.0
            # 初始化bash,让conda环境在新shell中可用
            conda init bash
      - run:
          name: install requirements
          command: |
            # 加载bash配置,激活conda环境
            source $HOME/.bashrc
            conda activate testenv
            pip install -r python-requirements.txt
            pip install pytest-ordering
      - run:
          name: download sample dataset
          command: |
            cd /home/circleci/repo/unit_tests/test_data
            wget http://cf.10xgenomics.com/samples/cell-exp/2.1.0/t_3k/t_3k_filtered_gene_bc_matrices.tar.gz
            tar -xvfz t_3k_filtered_gene_bc_matrices.tar.gz
      - run:
          name: run tests
          command: |
            source $HOME/.bashrc
            conda activate testenv
            pytest ./unit_tests
      - store_artifacts:
          path: test-reports
          destination: test-reports

额外提示

  • 方案1更适合CI场景,完全不依赖shell环境配置,避免各种隐藏的环境变量问题;
  • 方案2需要确保source $HOME/.bashrc能正确加载conda的初始化脚本,部分情况下可能需要调整bash的启动配置;
  • 后续如果需要添加新的run步骤,只要遵循对应方案的规则,就能保证始终使用conda环境里的Python版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:07:32