在新的终端窗口中从python执行终端命令?

时间:2021-01-05 02:48:10

The goal here is to run a new python file in a new shell from and existing python file in an existing shell. Say i have two files, aaa.py and bbb.py. Lets say for simplicity that all aaa.py does is...

这里的目标是在现有的shell中以新的shell和现有的python文件运行一个新的python文件。我有两个文件,aaa.py和bbb.py。让我们简单地说,所有的aaa.py都是…

subprocess.call('python bbb.py', shell=True)

...and lets say that bbb.py does is...

…假设bbb。py是……

print 'It worked'

Now the goal is to run aaa.py in terminal 1 and get it to launch bbb.py in terminal 2. I would expect something like the command below to exist, but can't figure it out.

现在的目标是在一号终端运行aaa.py,然后启动bbb。py终端2。我希望下面的命令可以存在,但不能解决。

subprocess.call_in_new_window('python bb.py', shell=True)

2 个解决方案

#1


20  

There's no way to do this in general from a shell. What you have to do is run the terminal program itself, or some launcher program that does so for you. And the way to do that is different for each terminal program.

没有办法从一个壳层来做这个。你需要做的是运行终端程序本身,或者一些为你做的启动程序。对于每个终端程序来说都是不同的。

In some cases, os.startfile will do what you want, but this isn't going to be universal.

在某些情况下,操作系统。startfile可以做你想做的事情,但这不会是通用的。

Also, note in general, you're going to actually need an absolute path to your script, because the new terminal window will be running a new shell and therefore won't necessarily have your same working directory. But I'll ignore that for the examples.

另外,一般来说,您实际上需要一个绝对路径到您的脚本,因为新的终端窗口将运行一个新的shell,因此不一定有相同的工作目录。但我将忽略这些例子。


With Windows cmd, the easiest way to do it is the start shell command. If the thing you start is any command-line program, including python, it will get a new cmd window. So, something like:

使用Windows cmd,最简单的方法是启动shell命令。如果您启动的东西是任何命令行程序,包括python,它将得到一个新的cmd窗口。所以,类似:

subprocess.call('start /wait python bb.py', shell=True)

OS X has a similar command, open. And it's a real program rather than a shell command, so you don't need shell=True. However, running a command-line program or script with open doesn't generally open a new terminal window. In fact, the whole point of it is to allow you to run programs as if they were being double-clicked in Finder, which never runs something in the terminal unless it's a .command file.

OS X有一个类似的命令,打开。它是一个真正的程序,而不是shell命令,所以您不需要shell=True。但是,运行一个带有open的命令行程序或脚本通常不会打开一个新的终端窗口。实际上,它的目的是让你运行程序,就像在Finder中双击一样,在终端中不运行任何东西,除非它是.command文件。

So, you can create a temporary .command wrapper file and open that; something like this (untested):

因此,您可以创建一个临时的.command包装文件并打开它;这样的(未测试):

with tempfile.NamedTemporaryFile(suffix='.command') as f:
    f.write('#!/bin/sh\npython bb.py\n')
    subprocess.call(['open', '-W', f.name])

Alternatively, you can explicitly tell open to use Terminal.app, something like this:

或者,您可以显式地告诉open使用终端。程序是这样的:

subprocess.call(['open', '-W', '-a', 'Terminal.app', 'python', '--args', 'bb.py'])

Or you can script Terminal.app via AppleEvents. For example:

或者你也可以使用脚本终端。通过AppleEvents应用。例如:

appscript.app('Terminal').do_script('python bb.py')

The "do script" event opens a new window and runs its argument as a command. If you want more detailed control, open the scripting dictionary in AppleScript Editor and see all the fun stuff you can do.

“do script”事件打开一个新窗口,并以命令的形式运行它的参数。如果您想要更详细的控制,请在AppleScript编辑器中打开脚本字典,并查看您能做的所有有趣的事情。


On Linux or other *nix systems… well, there are 65,102 different desktop environments, launchers, and terminal programs. Do you need to work on all of them?

在Linux或其他*nix系统上……嗯,有65,102个不同的桌面环境、发射器和终端程序。你需要对所有的人都工作吗?

With gnome-terminal, just running the terminal again gives you a new window, and the -x argument lets you specify an initial command, so:

使用gnome-terminal,运行终端会再次给您一个新的窗口,并且-x参数允许您指定一个初始命令,所以:

subprocess.call(['gnome-terminal', '-x', 'python bb.py'])

Many older terminals try to be compatible with xterm, which does the same thing with -e, so:

许多较老的终端都试图与xterm兼容,这与-e是一样的,所以:

subprocess.call(['xterm', '-e', 'python bb.py'])
subprocess.call(['rxvt', '-e', 'python bb.py'])

… etc.

…等。

How do you know which terminal the user is using? Good question. You could walk the like of parent processes from yourself until you find something that looks like a terminal. Or you could just assume everyone has xterm. Or you could look at how various distros configure a default terminal and search for all of them. Or…

您如何知道用户正在使用哪个终端?好问题。你可以从你自己的父进程中走出来,直到你找到一个看起来像终端的东西。或者你可以假设每个人都有xterm。或者,您可以查看不同的distros如何配置一个默认终端并搜索它们。还是……

#2


2  

You won't be able to make that happen, at least not as simply as you are thinking about it. I suspect that you are talking about a Mac because of "terminal window".

你无法做到这一点,至少不像你想的那样简单。我怀疑你说的是Mac因为“终端窗口”。

You might be able to make it happen using the X Window system, but you will need a bunch of things to be set up, X-servers, permissions, etc. to make that happen.

您可能会使用X Window系统来实现它,但是您需要设置一些东西,X-服务器、权限等等来实现这一点。

These days, such things usually violate normal security boundaries. Say you download a program that behaves as you suggest. It brings up a window (terminal) (invisible to you) that has the same privileges that you have. It proceeds to read all of your directories and files and ships them to the originator of the program. You might not be happy with that. The whole time, you think you are playing a game, then you quit, and the second shell keeps running.

这些天,这样的事情通常会违反正常的安全界限。假设你下载了一个你建议的程序。它打开了一个窗口(终端)(您看不到),它具有与您相同的权限。它将读取所有的目录和文件并将它们发送到程序的发起者。你可能不会满意。一直以来,你认为你在玩一个游戏,然后你退出,第二个shell继续运行。

Windows and shells are a bit disjoint.

窗户和贝壳有点脱节。

#1


20  

There's no way to do this in general from a shell. What you have to do is run the terminal program itself, or some launcher program that does so for you. And the way to do that is different for each terminal program.

没有办法从一个壳层来做这个。你需要做的是运行终端程序本身,或者一些为你做的启动程序。对于每个终端程序来说都是不同的。

In some cases, os.startfile will do what you want, but this isn't going to be universal.

在某些情况下,操作系统。startfile可以做你想做的事情,但这不会是通用的。

Also, note in general, you're going to actually need an absolute path to your script, because the new terminal window will be running a new shell and therefore won't necessarily have your same working directory. But I'll ignore that for the examples.

另外,一般来说,您实际上需要一个绝对路径到您的脚本,因为新的终端窗口将运行一个新的shell,因此不一定有相同的工作目录。但我将忽略这些例子。


With Windows cmd, the easiest way to do it is the start shell command. If the thing you start is any command-line program, including python, it will get a new cmd window. So, something like:

使用Windows cmd,最简单的方法是启动shell命令。如果您启动的东西是任何命令行程序,包括python,它将得到一个新的cmd窗口。所以,类似:

subprocess.call('start /wait python bb.py', shell=True)

OS X has a similar command, open. And it's a real program rather than a shell command, so you don't need shell=True. However, running a command-line program or script with open doesn't generally open a new terminal window. In fact, the whole point of it is to allow you to run programs as if they were being double-clicked in Finder, which never runs something in the terminal unless it's a .command file.

OS X有一个类似的命令,打开。它是一个真正的程序,而不是shell命令,所以您不需要shell=True。但是,运行一个带有open的命令行程序或脚本通常不会打开一个新的终端窗口。实际上,它的目的是让你运行程序,就像在Finder中双击一样,在终端中不运行任何东西,除非它是.command文件。

So, you can create a temporary .command wrapper file and open that; something like this (untested):

因此,您可以创建一个临时的.command包装文件并打开它;这样的(未测试):

with tempfile.NamedTemporaryFile(suffix='.command') as f:
    f.write('#!/bin/sh\npython bb.py\n')
    subprocess.call(['open', '-W', f.name])

Alternatively, you can explicitly tell open to use Terminal.app, something like this:

或者,您可以显式地告诉open使用终端。程序是这样的:

subprocess.call(['open', '-W', '-a', 'Terminal.app', 'python', '--args', 'bb.py'])

Or you can script Terminal.app via AppleEvents. For example:

或者你也可以使用脚本终端。通过AppleEvents应用。例如:

appscript.app('Terminal').do_script('python bb.py')

The "do script" event opens a new window and runs its argument as a command. If you want more detailed control, open the scripting dictionary in AppleScript Editor and see all the fun stuff you can do.

“do script”事件打开一个新窗口,并以命令的形式运行它的参数。如果您想要更详细的控制,请在AppleScript编辑器中打开脚本字典,并查看您能做的所有有趣的事情。


On Linux or other *nix systems… well, there are 65,102 different desktop environments, launchers, and terminal programs. Do you need to work on all of them?

在Linux或其他*nix系统上……嗯,有65,102个不同的桌面环境、发射器和终端程序。你需要对所有的人都工作吗?

With gnome-terminal, just running the terminal again gives you a new window, and the -x argument lets you specify an initial command, so:

使用gnome-terminal,运行终端会再次给您一个新的窗口,并且-x参数允许您指定一个初始命令,所以:

subprocess.call(['gnome-terminal', '-x', 'python bb.py'])

Many older terminals try to be compatible with xterm, which does the same thing with -e, so:

许多较老的终端都试图与xterm兼容,这与-e是一样的,所以:

subprocess.call(['xterm', '-e', 'python bb.py'])
subprocess.call(['rxvt', '-e', 'python bb.py'])

… etc.

…等。

How do you know which terminal the user is using? Good question. You could walk the like of parent processes from yourself until you find something that looks like a terminal. Or you could just assume everyone has xterm. Or you could look at how various distros configure a default terminal and search for all of them. Or…

您如何知道用户正在使用哪个终端?好问题。你可以从你自己的父进程中走出来,直到你找到一个看起来像终端的东西。或者你可以假设每个人都有xterm。或者,您可以查看不同的distros如何配置一个默认终端并搜索它们。还是……

#2


2  

You won't be able to make that happen, at least not as simply as you are thinking about it. I suspect that you are talking about a Mac because of "terminal window".

你无法做到这一点,至少不像你想的那样简单。我怀疑你说的是Mac因为“终端窗口”。

You might be able to make it happen using the X Window system, but you will need a bunch of things to be set up, X-servers, permissions, etc. to make that happen.

您可能会使用X Window系统来实现它,但是您需要设置一些东西,X-服务器、权限等等来实现这一点。

These days, such things usually violate normal security boundaries. Say you download a program that behaves as you suggest. It brings up a window (terminal) (invisible to you) that has the same privileges that you have. It proceeds to read all of your directories and files and ships them to the originator of the program. You might not be happy with that. The whole time, you think you are playing a game, then you quit, and the second shell keeps running.

这些天,这样的事情通常会违反正常的安全界限。假设你下载了一个你建议的程序。它打开了一个窗口(终端)(您看不到),它具有与您相同的权限。它将读取所有的目录和文件并将它们发送到程序的发起者。你可能不会满意。一直以来,你认为你在玩一个游戏,然后你退出,第二个shell继续运行。

Windows and shells are a bit disjoint.

窗户和贝壳有点脱节。