【问题标题】:Travis CI Python failed to import 'requests'Travis CI Python 无法导入“请求”
【发布时间】:2019-02-17 10:17:57
【问题描述】:

我想在 Travis CI 构建环境中运行一个简单的 Python 3 脚本。 我按照 Travis CI Python 指南 (https://docs.travis-ci.com/user/languages/python) 创建了一个初始 .travis.yml 文件。

Travis CI 在单独的 virtualenv 中针对 YML 文件中描述的 Python 版本运行每个构建。

我的脚本依赖于requests 模块,我将它添加到requirements.txt 文件中并pip 将其安装在Travis CI 中,但是一旦脚本运行,脚本就无法导入该模块。

错误

./benchmark.py https://graph.irail.be/sncb/connections -n 10 -i -o results.csv
Traceback (most recent call last):
File "./benchmark.py", line 3, in <module>
   import requests
ImportError: No module named 'requests'

.travis.yml

language: python
python:
   - "3.3"
   - "3.4"
   - "3.5"
   - "3.6"
# command to install deps
install:
   - pip install -r requirements.txt

# run build
script:
   - ./benchmark.py https://graph.irail.be/sncb/connections -n 10 -i -o results.csv

我的 Github 仓库:https://github.com/DylanVanAssche/http-benchmark/tree/feature/CI

我的 Travis CI 构建:https://travis-ci.com/DylanVanAssche/http-benchmark/jobs/145320050

【问题讨论】:

    标签: python python-requests travis-ci


    【解决方案1】:

    您需要专门致电python3 &lt;filename&gt; 以确保您使用的是python3。我不确定为什么会发生这种情况,但显然 #!/usr/bin/python3 指令不足以加载正确的模块。

    【讨论】:

    • 嗯,如果您专门使用python3 &lt;filename&gt; 而不是./filename 启动会怎样。我觉得我之前遇到过这个问题并明确调用 python3 修复它。
    • 谢谢!这就是我构建失败的原因。如果你更新你的答案,我会接受并关闭这个!
    • 所以是python和路径问题?
    • 您需要在文件之前调用 Python 解释器 (python3 myscript.py)。 shebang 行 (#!/usr/bin/python3) 在 Travis CI 中显然不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 2016-03-10
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    相关资源
    最近更新 更多