首页 > 解决方案 > Pytest 在具有 PyPI 和 conda 包依赖项的 Travis CI 测试中找不到 pyYAML

问题描述

我正在尝试使用 Travis CI为我的 Python 包设置自动测试。我的 Python 包依赖于Iris以及其他包,例如 PyYAML、numpy 等。它还依赖于 PyPI 包 ( ScriptEngine )。现在,我想使用 conda(安装 Iris)和 pip(安装 PyPI 包以及检查 PyYAML 和 numpy 的要求)设置 Travis CI 环境。然后我想使用pip install ..

为了测试这是否可行,我编写了一个导入 PyYAML 的简单 Pytest 测试。

我目前正在尝试使用此.travis.yml文件执行此操作:

language: python
python:
  - "3.6"
  - "3.7"
  - "3.8"
# command to install dependencies
install:
  - sudo apt-get update
  - wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
  - bash miniconda.sh -b -p $HOME/miniconda
  - source "$HOME/miniconda/etc/profile.d/conda.sh"
  - hash -r
  - conda config --set always_yes yes --set changeps1 no
  - conda update -q conda
  # Useful for debugging any issues with conda
  - conda info -a
  - conda env create -f tests/test-environment.yml python=$TRAVIS_PYTHON_VERSION
  - conda activate test-environment
  - conda install pip
  - conda install -c conda-forge iris
  - pip install -r requirements.txt
  - pip install .
# command to run tests
script: pytest

注意:这是我第一次真正使用 Travis CI。该脚本混合了来自Conda 文档和 Travis CI 文档的示例。

然后 Pytest 无法导入 PyYAML(尽管由于 requirements.txt 以及 Iris 依赖项而安装了它):以下是它已安装的日志确认:

Requirement already satisfied: pyYAML>=5.1 in /home/travis/miniconda/envs/test-environment/lib/python3.8/site-packages (from ece-4-monitoring==0.1.0) (5.3.1)

这是来自 Pytest 的错误:

$ pytest

============================= test session starts ==============================

platform linux -- Python 3.7.1, pytest-4.3.1, py-1.7.0, pluggy-0.8.0

rootdir: /home/travis/build/valentinaschueller/ece-4-monitoring, inifile:

collected 1 item / 1 errors                                                    

==================================== ERRORS ====================================

_________________ ERROR collecting tests/test_file_handling.py _________________

ImportError while importing test module '/home/travis/build/valentinaschueller/sciptengine-tasks-ecearth/tests/test_file_handling.py'.

Hint: make sure your test modules/packages have valid Python names.

Traceback:

tests/test_file_handling.py:3: in <module>

    import helpers.file_handling as file_handling

helpers/file_handling.py:1: in <module>

    import yaml

E   ModuleNotFoundError: No module named 'yaml'

!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!

=========================== 1 error in 0.12 seconds ============================

The command "pytest" exited with 2.

如果我在计算机上本地使用 conda 虚拟环境尝试此精确设置,则不会出现此问题。为什么这在 Travis CI 虚拟机上不起作用?

标签: pythonpipcondatravis-cipyyaml

解决方案


正如cel他们的评论中建议的那样:我可以通过在requirements.txt.

不需要test-environment在脚本部分激活。但是,一个非常有用的提示是使用echo $(which pytest) && pytest而不是仅仅pytest检查 pytest 是否安装在/home/travis/miniconda/envs/test-environment/bin/pytest.


推荐阅读