【问题标题】:python subprocess "no such file or directory" inside dockerdocker内部的python子进程“没有这样的文件或目录”
【发布时间】:2018-07-12 18:18:11
【问题描述】:

app.py

    def which(program):
        import os
        def is_exe(fpath):
            return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

        fpath, fname = os.path.split(program)
        if fpath:
            if is_exe(program):
                return program
        else:
            for path in os.environ["PATH"].split(os.pathsep):
                exe_file = os.path.join(path, program)
                if is_exe(exe_file):
                    return exe_file
        return None

    command = /some/path/to/command
    command = which(command)
    if command is not None:
        print "command exists and is exectuable"
        child = subprocess.Popen(command)

输出:

command exists and is exectuable
[Errno 2] No such file or directory

当它在 docker 中运行时,即使它可以找到可执行文件,当它通过子进程运行时,它会抛出“没有这样的文件”错误

当它在容器外运行时,我看不到这种行为

当命令通过子进程运行时,关于这里发生了什么的任何建议?当我添加 shell=True 时,它​​仍然找不到它

【问题讨论】:

  • 你的镜像是在 Ubuntu 上构建的吗?
  • @aerokite centos
  • ls -la /some/path/to/command ?
  • 我以前见过这个问题。可能是您没有运行该命令的权限,或者该命令不可执行。
  • 但是 which() 不能确保它是可执行的并且我可以运行它吗?现在我得到 /bin/sh: 1: ls: not found

标签: python docker centos


【解决方案1】:

我也遇到过这个问题。我有script_1 和hashbang #!/usr/bin/env python3,它叫Popen(['script_2']),它有hashbang #!/bin/env python3。 Popen() 报告了[Errno 2] No such file or directory: '/path/to/script_2': '/path/to/script_2',但实际上不存在/bin/env 的问题。我更正了 script_2 的 hashbang 以使用 /usr/bin/env 来解决问题。

【讨论】:

    猜你喜欢
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 2022-01-15
    • 2019-01-22
    相关资源
    最近更新 更多