应用程序.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 时,它仍然找不到它
我也遇到过这个问题。我有
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
来解决问题。
当我尝试从 Alpine 基础映像构建 Golang 可执行文件并尝试在 python:3.9-slim 基础映像上复制并执行它时,我遇到了同样的问题。即使文件存在于可执行 mod 中,我也收到 FileNoteFound 错误。
发生这种情况是因为程序是为一种环境编译的,并在另一种环境中使用。
以下解决方案对我有用。
我需要通过设置 GOOS 环境变量来构建 Linux 的可执行文件。
所以不要像这样构建我的二进制文件:
RUN go install github.com/google/osv-scanner/cmd/osv-scanner@v1
它应该像这样构建:
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on \
go install github.com/google/osv-scanner/cmd/osv-scanner@v1