gpt4 book ai didi

python - 子进程无法在非交互模式下运行 python 可执行文件?

转载 作者:太空宇宙 更新时间:2023-11-04 02:26:02 26 4
gpt4 key购买 nike

我正在使用 python 3.6.3 和 subprocess 模块来运行另一个 python 脚本

#  main.py

#!/bin/env python
from subprocess import Popen,PIPE
from sys import executable

p = Popen([executable, 'test.py', 'arg1'],shell=True, stdout=PIPE)
p.wait()
print(p.stdout.read().decode())

#  test.py

import sys
print(sys.argv)

我希望它能运行并执行 test.py。但是,它会以交互模式打开 python 解释器!

我测试了 shell=False 选项,它有效。我测试了 args 的字符串形式而不是列表形式,它有效。

不知道是不是bug

最佳答案

您需要删除 shell=True 或将第一个参数更改为 executable + 'test.py arg1' 而不是 [executable, 'test. py', 'arg1'].

the documentation 中所述,使用 shell = True,它将以 /bin/sh -c python test.py arg1 运行它,这意味着 python 将运行没有争论。

关于python - 子进程无法在非交互模式下运行 python 可执行文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50397391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com