简体   繁体   English

从python启动新shell窗口中的程序

[英]launch program in new shell window from python

I am trying to launch a python script from another python script in a new shell window. 我试图在新的shell窗口中从另一个python脚本启动python脚本。 So far I'm unable to do it. 到目前为止我无法做到。 Does anyone knows how can I accomplish this? 有谁知道我怎么能做到这一点?

for example 例如

import subprocess
process = subprocess.Popen('test.py', shell=True, stdout=subprocess.PIPE)
process.wait()
print (process.returncode)

when I'll run this script, it should launch 'test.py' in a new a new shell window. 当我运行这个脚本时,它应该在一个新的shell窗口中启动'test.py'。

I'm using linux, but it will be very helpful if you can provide solution for windows too. 我正在使用linux,但如果你也可以为windows提供解决方案,那将非常有用。

Here's how you could do it on Debian-like systems: 以下是如何在类似Debian的系统上执行此操作:

import subprocess
import shlex
process = subprocess.Popen(
    shlex.split("""x-terminal-emulator -e 'bash -c "test.py"'"""), stdout=subprocess.PIPE)
process.wait()
print (process.returncode)

Something like it should work for any *nix system. 类似的东西应该适用于任何* nix系统。

Many thanks to eudoxos for pointing out x-terminal-emulator ! 非常感谢eudoxos指出x-terminal-emulator

Instead of launching a shell, launch a terminal running your script. 启动运行脚本的终端,而不是启动shell。 On Linux, xterm -e test.py ; 在Linux上, xterm -e test.py ; the Windows equivalent would be cmd.exe test.py I believe (but I could be wrong). Windows等效的将是cmd.exe test.py我相信(但我可能是错的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM