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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19308415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:24:30  来源:igfitidea点击:

Execute terminal command from python in new terminal window?

pythonshellterminalsubprocessnew-window

提问by Evan Cathcart

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 中的现有 python 文件和新 shell 中运行一个新的 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.

现在的目标是在终端 1 中运行 aaa.py 并让它在终端 2 中启动 bbb.py。我希望存在类似下面的命令,但无法弄清楚。

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

采纳答案by abarnert

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.

通常无法从 shell 执行此操作。您需要做的是运行终端程序本身,或一些为您执行此操作的启动程序。对于每个终端程序来说,这样做的方法是不同的。

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

在某些情况下,os.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 startshell command. If the thing you startis any command-line program, including python, it will get a new cmd window. So, something like:

使用 Windows cmd,最简单的方法是使用startshell 命令。如果您使用的start是任何命令行程序,包括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 opendoesn'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 有一个类似的命令,open. 它是一个真正的程序而不是一个 shell 命令,所以你不需要shell=True. 但是,运行命令行程序或脚本open通常不会打开新的终端窗口。事实上,它的全部意义在于允许您像在 Finder 中双击程序一样运行程序,除非它是 .command 文件,否则它永远不会在终端中运行任何东西。

So, you can create a temporary .commandwrapper file and openthat; something like this (untested):

因此,您可以创建一个临时.command包装文件open;像这样(未经测试):

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 opento use Terminal.app, something like this:

或者,您可以明确告诉open使用 Terminal.app,如下所示:

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

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

或者您可以通过 AppleEvents 编写 Terminal.app 脚本。例如:

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 -xargument 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. 或者您可以查看各种发行版如何配置默认终端并搜索所有这些。或者…

回答by Fred Mitchell

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.

Windows 和 shell 有点不相交。

回答by Danyal

This should probably be a comment, but since I can't yet...

这可能应该是一个评论,但因为我还不能......

In Windows , you can do:

在 Windows 中,您可以执行以下操作:

subprocess.call('python bb.py', creationflags=subprocess.CREATE_NEW_CONSOLE)