如何从pycharm中的脚本打开编辑器?

时间:2018-06-04 16:35:53

标签: python subprocess pycharm

我正在尝试在pycharm的终端中打开一个编辑器,运行下面的脚本并收到错误:

  

打开终端时出错:未知。

EDITOR env var设置为/usr/bin/nano

在下面的MVE中正确读取了env var。

如果我直接从pycharm终端命令行调用$EDITOR,我可以打开编辑器。

如果从pycharm内部或外部的新终端窗口调用脚本,该脚本将起作用。

只有从运行配置运行时才会出现问题。

import os
import subprocess


def main():
    editor = os.getenv('EDITOR')
    with open('new.txt', 'w+') as tmp:
        subprocess.call([editor, tmp.name])


if __name__ == "__main__":
    main()

我发现了一些相关的问题,但没有解释问题的原因。

1 个答案:

答案 0 :(得分:0)

The answer seems to be to check the [✓] Emulate terminal in output console checkbox in the Run/Debug Configurations window. This doesn't quite produce a functioning terminal session (there are issues with not seeing keyboard inputs after exiting the editor), but it works just well enough for my debugging purposes.