如何在 PyCharm IDE 中使用子进程模块调用基于 ncurses 的应用程序? [英] How to call an ncurses based application using subprocess module in PyCharm IDE?

查看:54
本文介绍了如何在 PyCharm IDE 中使用子进程模块调用基于 ncurses 的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 subprocess 模块从 python 启动一个基于 ncurses 的应用程序.

I would like to launch an ncurses based application from python using subprocess module.

基于 ncurses 的应用程序是 TABARI,一个事件提取系统.事件提取的结果保存到文件中.我想从 python 脚本启动它,等待它终止,然后读取结果文件.

The ncurses based application is TABARI, an event extraction system. The result of event extraction is saved to a file. I would like to launch it from a python script, wait for it to terminate and then read the results file.

代码示例如下所示:

import subprocess
proc = subprocess.Popen('TABARI -a ' + file, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print proc.communicate()

这段代码在运行PyCharm程序时的结果是:

The result of this code when running the program is PyCharm is:

('', 'Error opening terminal: unknown.\n')

当我从终端启动的 python 解释器(与 PyCharm 中使用的相同)运行相同的代码时,输​​出是:

When I run the same code from a terminal initiated python interpreter (the same as is used within PyCharm), the output is:

('...lots of text...', '')

我尝试了几种方法,包括使用 shell=False、将 bufsize 设置为 -1 以及调查 os.environ 变量.PyCharm 的 os.environ 输出和终端之间的一个可疑差异是TERM"变量,它在 PyCharm 中不存在,在终端中等于xterm".

I tried several things, including using shell=False, setting the bufsize to -1, and investigating os.environ variables. One suspicious difference between the os.environ output from PyCharm and the terminal is the 'TERM' variable, which does not exist in PyCharm and equals 'xterm' in terminal.

我将不胜感激.

推荐答案

我不知道 PyCharm 或 TABARI,但从错误消息看来,PyCharm 正在执行您的代码,而没有将其连接到终端.可能它这样做是为了收集程序输出并将其显示在 GUI 窗口中,或者因为作者认为启动像 xterm 这样的终端模拟器并在其中运行代码不是很干净.

I don't know PyCharm or TABARI specifically, but from the error message it sounds like PyCharm is executing your code without connecting it to a terminal. Possibly it does this so it can just collect program output and display it in a GUI window, or because the authors don't feel like it's very clean to launch a terminal emulator like xterm and run your code inside that.

从这里的其他一些问题来看,似乎没有什么真正好的方法可以让 PyC​​harm 在运行代码时提供终端仿真环境.有一些建议 在这个问题上,但听起来不太令人满意.

From some of the other questions around here, it sounds like there isn't any really good way to make PyCharm provide a terminal-emulation environment when running your code. There are some suggestions on this question, but they don't sound very satisfactory.

阻力最小的路径可能只是每次都从终端运行您的程序.如果这是不可接受的,您可以让您的代码检查 stdin 是否是终端 (os.isatty(0)),如果不是,则显式启动终端模拟器,如 xterm 并重新调用您的代码在那之下.或者,如果您实际上不需要在运行时与子进程交互,则可以分配自己的伪终端主/从对并运行连接到从属的代码.这些事情都比它们应有的更复杂,对所有这些内容的完整解释需要足够的文字来填满整个手册,但这里有一些很好的资源:

The path of least resistance is probably just to run your program from the terminal each time. If that's unacceptable, you could have your code check to see if stdin is a terminal (os.isatty(0)), and if not, explicitly launch a terminal emulator like xterm and re-invoke your code under that. Or, if you don't actually need to interact with the subprocess while it runs, you could allocate your own pseudoterminal master/slave pair and run the code connected to the slave. These things are all more complicated than they probably should be, and a full explanation of all of it would take enough text to fill a whole manual, but here are some good resources:

  • Wikipedia entry on Pseudo Terminals, for some very general background
  • man page for xterm(1), for info on how to launch with a particular command instead of your shell
  • man page for pty(7)- explains the mechanics of interacting with pty/tty devices
  • the Python pty module, in case you want to make a pseudoterminal master/slave pair and interact with it from plain Python
  • an explanation from an old-ish Linux Kernel manual regarding how process groups and sessions relate to terminal ownership
  • an excerpt from Advanced Programming in the UNIX® Environment: Second Edition By W. Richard Stevens, Stephen A. Rago with some more info about terminal control

这篇关于如何在 PyCharm IDE 中使用子进程模块调用基于 ncurses 的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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