Pytest在具有PyPI和conda包依赖项的Travis CI测试中找不到pyYAML

2024-05-13 23:51:44 发布

您现在位置:Python中文网/ 问答频道 /正文

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

为了测试这是否有效,我编写了一个简单的Pytest测试来导入PyYAML

我目前正试图使用此.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 docs和Travis CI文档的示例

Pytest随后无法导入PyYAML(尽管由于requirements.txt和Iris依赖关系而安装了PyYAML): 以下是日志中的安装确认信息:

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虚拟机上不起作用


Tags: installpippytesttraviscipytestsh
1条回答
网友
1楼 · 发布于 2024-05-13 23:51:44

正如their comment中的cel所建议的那样:我可以通过在requirements.txt中显式地要求pytest来解决这个问题

无需在脚本部分中激活test-environment。然而,一个非常有用的技巧是使用echo $(which pytest) && pytest而不是仅仅使用pytest来检查pytest是否安装在/home/travis/miniconda/envs/test-environment/bin/pytest

相关问题 更多 >