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时,它仍然无法找到它
答案 0 :(得分:0)
我也遇到了这个问题。我有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
来解决此问题。