擅长:python、mysql、java
<p>如果有Python解释器设置来处理*.py文件,那么只需在Popen构造函数中设置<code>shell=True</code>:</p>
<pre><code>child_program = subprocess.Popen("child.py",
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
</code></pre>
<p><strong>正如其他人所指出的,这个方法带有一个安全警告</strong>,因此您必须注意传递给Popen的参数不是恶意构造的(例如,如果您从用户输入中获取一些参数):<a href="https://docs.python.org/3/library/subprocess.html#security-considerations" rel="nofollow">https://docs.python.org/3/library/subprocess.html#security-considerations</a></p>
<p>否则,您需要告诉Popen使用Python可执行文件来加载文件,如下所示:</p>
^{pr2}$
<p>在此上下文中,<code>sys.executable</code>将解析为用于启动父脚本的Python二进制文件的完整路径。在</p>