Skip to main content
 首页 » 编程设计

python之带有 anaconda 和 travis 的多个 python 版本

2025年02月15日10jirigala

我有一个 python 项目,它使用需要构建的库。鉴于我使用 python 。我想为 travis 创建一个计划,让我可以针对多个 python 版本进行测试,但我无法做到。这是我所拥有的:

  • 我想针对多个 python 版本(例如 2.7、3.5、3.6)对其进行测试
  • 我有 requirements.yml 文件,如下所示:
  channels: 
       - kne  
  dependencies: 
       - numpy 
       - pytest 
       - numpy 
       - scipy 
       - matplotlib 
       - seaborn 
       - pybox2d 
       - pip: 
        - gym 
        - codecov 
        - pytest 
        - pytest-cov 

我的 .travis.yml 包含:

language: python 
 
# sudo false implies containerized builds 
sudo: false 
 
python: 
  - 3.5 
  - 3.4 
 
before_install: 
# Here we download miniconda and install the dependencies 
- export MINICONDA=$HOME/miniconda 
- export PATH="$MINICONDA/bin:$PATH" 
- hash -r 
- wget http://repo.continuum.io/miniconda/Miniconda-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 
- echo "Python version var" 
- echo $TRAVIS_PYTHON_VERSION 
- conda env create -n testenv -f environment.yml python=$TRAVIS_PYTHON_VERSION 
- source activate testenv 
 
install: 
- python setup.py install 
 
script: 
- python --version 
- python -m pytest --cov=. 
- codecov 

如果我将 python 版本放入 environment.yml 它工作正常但我不能使用多个 python 版本。对我来说,似乎如果提供了 -f,它会忽略为 conda env create 列出的任何其他包。

此外,在创建环境后添加 - conda install -n testenv python=$TRAVIS_PYTHON_VERSION 不起作用。

UnsatisfiableError: The following specifications were found to be in conflict: 
  - functools32 -> python 2.7.* 
  - python 3.5* 
Use "conda info <package>" to see the dependencies for each package. 

我应该怎么做才能让它发挥作用?

//如果您想查看更多详细信息,请访问此处:https://travis-ci.org/mbednarski/Chiron/jobs/220644726

请您参考如下方法:

创建conda环境前可以使用sed修改environment.yml文件中的python依赖。

在environment.yml中包含python:

channels: 
    - kne  
dependencies: 
    - python=3.6 
    - numpy 
    - pytest 
    - numpy 
    - scipy 
    - matplotlib 
    - seaborn 
    - pybox2d 
    - pip: 
        - gym 
        - codecov 
        - pytest 
        - pytest-cov 

然后修改你的.travis.yml:

before_install: 
    # Here we download miniconda and install the dependencies 
    - export MINICONDA=$HOME/miniconda 
    - export PATH="$MINICONDA/bin:$PATH" 
    - hash -r 
    - wget http://repo.continuum.io/miniconda/Miniconda-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 
    - echo "Python version var" 
    - echo $TRAVIS_PYTHON_VERSION 
    # Edit the environment.yml file for the target Python version 
    - sed -i -E 's/(python=)(.*)/\1'$TRAVIS_PYTHON_VERSION'/' ./environment.yml 
    - conda env create -n testenv -f environment.yml 
    - source activate testenv 

sed 正则表达式会将文本 python=3.6 替换为目标 python 版本的等效项。

顺便说一句:我在您的存储库中看到您已经通过使用多个 environment.yml 文件解决了这个问题。这似乎是合理的,甚至对于某些依赖项是必要的,但对于许多 Python 版本来说维护起来可能很乏味。