使用 shebang 使用 subprocess.call 执行 python 脚本 [英] Executing python scripts with subprocess.call using shebang

查看:59
本文介绍了使用 shebang 使用 subprocess.call 执行 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Python 3 编写一个(有点)模块化的应用程序,我想从中运行任意程序,所述程序是在运行时指定的,不一定是 Python 脚本.

I'm writing a (somewhat) modular application in Python 3 and I'd like to run arbitrary programs from it, said program being specified at runtime and not necessarily a python script.

所以我使用例如,

subprocess.call([spam, "-i", eggs, "-o", ham])

如果 spam 是一个 python 脚本,有 shebang 到 python3 和可执行权限,我得到

If spam is a python script, with shebang to python3 and executable rights, I get

OSError: [Errno 8] Exec format error

如果我

subprocess.call(["python3", spam, "-i", eggs, "-o", ham])

效果很好.

你知道为什么吗?如何在不指定 python3 的情况下运行 spam?

Do you know why? How can I run spam without specifying python3?

推荐答案

你需要使用shell=True,并且你需要把你的数组变成一个命令字符串,像这样:

You need to use shell=True, and you need your array to be turned into a command string, like this:

subprocess.call(' '.join([spam, "-i", eggs, "-o", ham]), shell=True)

这将调用 shell 而不是直接命令,并且 shell 应该能够处理 shebang.

This will invoke the shell instead of the direct command, and the shell should be able to handle the shebang.

这篇关于使用 shebang 使用 subprocess.call 执行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆