【问题标题】:How to allocate a pseudo-tty in which to run scripts from IDE?如何分配从 IDE 运行脚本的伪 tty?
【发布时间】:2017-12-04 17:17:13
【问题描述】:

Eclipse(和其他 IDE,例如 Pycharm/IntelliJ)似乎没有使用合适的终端来运行程序。当程序需要使用正确的标准输入/标准输出时,这可能会导致一些问题。

例如简单的python脚本

# Exit upon any keypress

import sys
import termios
import tty

fd = sys.stdin.fileno()
tty.setraw(sys.stdin.fileno())

while not sys.stdin.read(1):
    pass

直接从 bash 运行时会正常运行,但从任何 IDE 运行时会失败并出现以下错误

Traceback (most recent call last):
  File "/Users/anishmoorthy/Code/termserver/turst.py", line 19, in <module>
    while readchar() is None:
  File "/Users/anishmoorthy/Code/termserver/turst.py", line 10, in readchar
    tty.setraw(sys.stdin.fileno())
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tty.py", line 20, in setraw
    mode = tcgetattr(fd)
termios.error: (25, 'Inappropriate ioctl for device')

更一般地说,任何对tcgetattr(&lt;stdin&gt;) 的调用都会失败。

我想知道是否有任何插件或设置,Eclipse 或 IntelliJ,它们可以通过在适当的 bash 进程中运行脚本来修复这些错误。

IntelliJ 实际上在其运行配置选项中有一个“在输出控制台中模拟终端”复选框,它修复了这些错误,但由于某种原因,它没有注册我的输入键 - 使其或多或少无用

【问题讨论】:

    标签: eclipse intellij-idea terminal console pycharm


    【解决方案1】:

    对于 IntelliJ IDEA 和 PyCharm,有一个隐藏选项可以通过添加来启用

    -Drun.processes.with.pty=true
    

    Help | 编辑自定义虚拟机选项

    See here 用于相关请求。

    【讨论】:

    • 这可以防止错误发生,所以我将其标记为答案。但是,此选项似乎启用了某种行缓冲。我发布的脚本在通过 bash 运行时会立即检测到按键,但是,当通过 pycharm 运行时,您实际上必须按 Enter 才能发送您的输入(因此脚本实际上检测到两个按键)。我想您不知道有任何其他方法可以禁用此新行为吗?
    • 更好的方法是使用真正的终端。您可以设置一个外部工具并将其绑定到某个键盘快捷键,该快捷键将在外部终端窗口中运行活动的编辑器脚本。另请参阅this request
    猜你喜欢
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 2010-10-25
    相关资源
    最近更新 更多