【问题标题】:Python subprocess check_output FileNotFoundError: [Errno 2] No such file or directoryPython subprocess check_output FileNotFoundError: [Errno 2] No such file or directory
【发布时间】:2019-05-17 09:14:58
【问题描述】:

我编写了一个小 Python 脚本来构建和测试我的那个项目的 docker 镜像。该脚本在本地运行良好,但在 check_output 期间远程失败。

构建.py

    print(check_output(["which", "dgoss"]))
    result = str(check_output(["dgoss", "run", image_tag], cwd=image_path, stderr=subprocess.DEVNULL))

.gitlab-ci.yml

    before_script:
      - curl -fsSL https://goss.rocks/install | sh
      - ls -la /usr/local/bin/ | grep goss
    script:
      - which dgoss
      - python3 ./infrastructure/build.py

输出

dgoss master has been installed to /usr/local/bin/dgoss
$ ls -la /usr/local/bin/ | grep goss
-rwxr-xr-x    1 root     root          3803 May 17 08:58 dgoss
-rwxr-xr-x    1 root     root       8524064 May 17 08:58 goss
$ which dgoss
/usr/local/bin/dgoss
$ python3 ./infrastructure/build.py
infrastructure/broker
b'/usr/local/bin/dgoss\n'
Traceback (most recent call last):
  File "./infrastructure/build.py", line 54, in <module>
    test()
  File "./infrastructure/build.py", line 42, in test
    result = str(check_output(["dgoss", "run", image_tag], cwd=image_path, stderr=subprocess.DEVNULL))
  File "/usr/lib/python3.6/subprocess.py", line 356, in check_output
    **kwargs).stdout
  File "/usr/lib/python3.6/subprocess.py", line 423, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'dgoss': 'dgoss'

文件存在且可执行。 python为什么会抛出异常?

【问题讨论】:

  • check_output 做什么?明明找了一个文件名dgoss,找不到,这个文件是什么?
  • 您应该尝试使用完整路径调用dgoss 命令。像这样:result = str(check_output([str(check_output(["which", "dgoss"])).strip(), "run", image_tag],... 当然,你可以为dgoss的路径创建一个分隔变量。
  • 试过完整路径,也没用。

标签: python python-3.x subprocess gitlab-ci gitlab-ci-runner


【解决方案1】:

原来 dgoss 是一个围绕 goss 的 bash 包装器,并且没有 bash 的 alpine docker 图像发布。

before_script:
  - apk add curl python3 bash

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多