gpt4 book ai didi

linux - Travis CI 中 Python 模块的 ImportError

转载 作者:太空宇宙 更新时间:2023-11-04 11:43:58 25 4
gpt4 key购买 nike

我在 .travis.yml 中有一组脚本,目前运行良好

...
install:
- scripts/travis/install_deps.sh
- virtualenv -p /opt/pyenv/versions/3.6/bin/python3.6 venv
- source venv/bin/activate
- pip install -r requirements.txt
before_script:
- scripts/test.sh
script:
- scripts/travis/build.sh
after_success:
- deactivate
- virtualenv -p /opt/pyenv/versions/2.7/bin/python2.7 venv
- source venv/bin/activate
- pip install -r requirements.txt
...

但是,我想稍微清理一下,这样就可以减少重复,使 .travis.yml 看起来像

...
install:
- scripts/travis/install_deps.sh
- export PYTHON_VERSION=3.6
- scripts/travis/install_python_deps.sh
before_script:
- scripts/test.sh
script:
- scripts/travis/build.sh
after_success:
- export PYTHON_VERSION=2.7
- scripts/travis/install_python_deps.sh
...

install_python_deps.sh 看起来像

#!/usr/bin/env bash
set -e

if [ ! -z "$VIRTUAL_ENV" ]; then deactivate; fi
virtualenv -p "/opt/pyenv/versions/${PYTHON_VERSION}/bin/python${PYTHON_VERSION}" venv
source venv/bin/activate
pip install -r requirements.txt

在 travis 中运行时会出现问题。当 test.sh 运行依赖于 requirements.txt 中声明的模块的 python 脚本未找到时,构建中断。任何关于为什么会发生这种情况的指示将不胜感激。

最佳答案

scripts/travis/install_python_deps.sh 中的 source venv/bin/activate 仅在脚本 install_python_deps.sh 退出之前有效。如果你想在 install_python_deps.sh 脚本之外使用已安装的模块,您也需要在脚本之外运行 source venv/bin/activate(再次),例如:

...
install:
- scripts/travis/install_deps.sh
- scripts/travis/install_python_deps.sh 3.6
- source venv/bin/activate
before_script:
- scripts/test.sh
script:
- scripts/travis/build.sh
after_success:
- scripts/travis/install_python_deps.sh 2.7
- source venv/bin/activate
...

请注意,为了使其更短,我用命令行参数替换了 PYTHON_VERSION 环境变量。您可以相应地调整 scripts/travis/install_python_deps.sh 脚本:

#!/usr/bin/env bash

set -euo pipefail

PYTHON_VERSION=$1

if [ "$VIRTUAL_ENV" ]; then deactivate; fi

virtualenv -p "/opt/pyenv/versions/${PYTHON_VERSION}/bin/python${PYTHON_VERSION}" venv
source venv/bin/activate
pip install -r requirements.txt

关于linux - Travis CI 中 Python 模块的 ImportError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58436881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com