在新的终端窗口中从 python 执行终端命令? [英] Execute terminal command from python in new terminal window?

查看:20
本文介绍了在新的终端窗口中从 python 执行终端命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的目标是从现有 shell 中的现有 python 文件和现有 shell 中的新 shell 中运行新的 python 文件.假设我有两个文件,aaa.py 和 bbb.py.为简单起见,我们可以说 aaa.py 所做的只是...

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...

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

...让我们说 bbb.py 确实是...

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

print 'It worked'

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

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.

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

推荐答案

通常无法从 shell 执行此操作.您需要做的是运行终端程序本身,或一些为您执行此操作的启动程序.并且每个终端程序的方法都不同.

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.

在某些情况下,os.startfile做你想做的,但这不会是普遍的.

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

另外,请注意,一般来说,您实际上需要一个脚本的绝对路径,因为新的终端窗口将运行一个新的 shell,因此不一定具有相同的工作目录.但我会在示例中忽略这一点.

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.

使用 Windows cmd,最简单的方法是 start shell 命令.如果你启动的东西是任何命令行程序,包括python,它会得到一个新的cmd窗口.所以,就像:

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:

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

<小时>

OS X 有一个类似的命令,open.它是一个真正的程序而不是一个 shell 命令,所以你不需要 shell=True.但是,使用 open 运行命令行程序或脚本通常不会打开新的终端窗口.事实上,它的全部意义在于允许您像在 Finder 中双击它们一样运行程序,除非它是 .command 文件,否则它永远不会在终端中运行任何东西.


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.

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

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

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

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

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

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

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

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

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

do script"事件打开一个新窗口并作为命令运行它的参数.如果您想要更详细的控制,请在 AppleScript 编辑器中打开脚本词典,看看您可以做的所有有趣的事情.

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.

在 Linux 或其他 *nix 系统上……嗯,有 65,102 种不同的桌面环境、启动器和终端程序.你需要处理所有这些吗?

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?

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

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

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

许多旧终端尝试与 xterm 兼容,它与 -e 做同样的事情,所以:

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

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

……等等

你怎么知道用户使用的是哪个终端?好问题.您可以从自己身上走类似父进程的过程,直到找到看起来像终端的东西.或者你可以假设每个人都有 xterm.或者您可以查看各种发行版如何配置默认终端并搜索所有终端.或者……

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…

这篇关于在新的终端窗口中从 python 执行终端命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆