Skip to main content
 首页 » 编程设计

python之Travis CI python 导入不起作用

2025年05月04日14zfyouxi

在我的项目中,我在 Travis CI 上的测试脚本有问题。我使用 conda 安装了我需要的包,然后运行我的测试脚本。由于导入错误,构建失败。我怎样才能解决这个问题?

我的 travis YAML 文件是这样的:

language: python 
python: 
  - "2.7" 
  - "3.4" 
  - "nightly" 
 
# command to install dependencies 
# setup anaconda 
before_install: 
  - wget http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh 
  - chmod +x miniconda.sh 
  - bash miniconda.sh -b -p $HOME/miniconda 
  - export PATH="$HOME/miniconda/bin:$PATH" 
  - conda update -y conda 
 
# install packages 
install: 
  - conda install -y numpy scipy matplotlib networkx biopython 
  - echo $PATH 
  - which python 
 
 
# command to run tests 
script: py.test 

我的一个测试脚本需要 BioPython,但测试脚本失败,因为它找不到 biopython。

__________________ ERROR collecting test_genotype_network.py ___________________ 
test_genotype_network.py:1: in <module> 
    import genotype_network as gn 
genotype_network.py:1: in <module> 
    from Bio import SeqIO 
E   ImportError: No module named 'Bio' 

有办法解决这个问题吗?

请您参考如下方法:

原来问题出在 py.test - 我没有在 conda 环境中安装它。在创建新环境时将 pytest 添加到要安装的包中,这些导入错误就消失了。