IngoMeyer441 / simple-term-menu

A Python package which creates simple interactive menus on the command line.
MIT License
492 stars 43 forks source link

Running in PyCharm throws termios.error: (5, 'Input/output error') #81

Closed sla-te closed 10 months ago

sla-te commented 11 months ago

Start Pycharm, create a new file with the following content and hit "Run"

def main():
    terminal_menu = simple_term_menu.TerminalMenu(["entry 1", "entry 2", "entry 3"])
    menu_entry_index = terminal_menu.show()
    print(f"You choose: {menu_entry_index}")

if __name__ == "__main__":
    main()

Traceback (most recent call last):
  File "/.../tmp.py", line 19, in <module>
    main()
  File "/.../tmp.py", line 13, in main
    menu_entry_index = terminal_menu.show()
                       ^^^^^^^^^^^^^^^^^^^^
  File ".../venv/lib/python3.11/site-packages/simple_term_menu.py", line 1497, in show
    self._init_term()
  File ".../venv/lib/python3.11/site-packages/simple_term_menu.py", line 961, in _init_term
    termios.tcsetattr(
termios.error: (5, 'Input/output error')
****
IngoMeyer441 commented 11 months ago

Thanks for the bug report. By default, PyCharm does not create a full terminal instance which is needed by simple-term-menu, thus raising the error, you got. You can enable emulating a full terminal emulator in the output console as a workaround. See https://intellij-support.jetbrains.com/hc/en-us/community/posts/360003383619/comments/6589796593042 for details.

sla-te commented 11 months ago

Thank you for the explanation and I can confirm, that it works if activating terminal emulation in the run configuration. Do note though, that on linux hitting run actually sometimes even crashes the entire IDE.

sla-te commented 11 months ago

Another drawback is, that if activating terminal emulation in PyCharm we cannot use its built in debugger anymore btw.

IngoMeyer441 commented 11 months ago

Ok I see. But the main problem is, that simple-tem-menu relies on being run in a terminal emulator. Without the terminal emulation option, pyCharm uses simple stdin and stdout redirection (as far as I know) and simple-term-menu cannot work with that.

sla-te commented 11 months ago

Ah well, we just have to remember to activate it before running - its a bit cumbersome though, because then we have to manually create a run config and right click run file will not work anymore - maybe it would be easy to add some kind of "debug" mode?

IngoMeyer441 commented 11 months ago

How shall the debug mode look like?

sla-te commented 11 months ago

Well something, that gives less functionality but wont crash Pycharm ^^

IngoMeyer441 commented 11 months ago

Hmm, difficult, since simple-term-menu needs terminal capabilities for redrawing the screen... maybe it can be combined with pyte in this special case? I am quite unsure about this.

sla-te commented 11 months ago

Well, maybe for starters: Can we somehow detect, that the current terminal is not suitable and then throw before the entire IDE crashes? That would, at least for me, help big time, because if you get unlucky and PyCharm crashes like this too often, you have to reinstall it completely (already had to 2 times), because of yea.. I wish I know but PyCharms error console was full of plugin errors, that would not disappear anymore.

Next step I think would be trying to find out how exactly PyCharms console or rather "terminal emulator" works internally and then investigate further how we can get the functionality we need or disable features, that are not compatible.

IngoMeyer441 commented 11 months ago

Ah ok, I thought the IDE would only crash sometimes with terminal emulation mode turned on. So it crashes without terminal emulation?

sla-te commented 11 months ago

Exactly, it never crashes if terminal emulation is activated, it only crashes if not (thats the default) and that pretty consistently.

IngoMeyer441 commented 11 months ago

I have run some tests in PyCharm. In the latest PyCharm version, the output console seems to always have some kind of terminal emulation and terminal emulation cannot be switched on separately. However, no menu is shown :disappointed:. Therefore, I have added a check for the PyCharm output console on branch fix/pycharm-console and abort the execution. Is this ok for now? You can test this change by getting the patched package version:

pip install 'git+https://github.com/IngoMeyer441/simple-term-menu@fix/pycharm-console'
sla-te commented 11 months ago

Sorry for the late reply. It does block execution but also blocks it if emulate terminal output in console is enabled, could you determine whether or not it is enabled and only throw if not, else it would not run in Pycharm at all.

IngoMeyer441 commented 10 months ago

The emulate terminal output in console checkbox is missing in the latest PyCharm release. Do you still use an older version?

sla-te commented 10 months ago

Screenshot_20231019_120657 It just moved, its still there

IngoMeyer441 commented 10 months ago

Thanks, I didn't find this. I have updated the branch and added a check for the TERM variable. It can be used to detect a terminal in the PyCharm output console. However, there is still an issue left: If PyCharm itself is started from a terminal emulator, the TERM variable is always set and the detection won't work.

IngoMeyer441 commented 10 months ago

The fix is added in release v1.6.2. If it still needs improvement, please feel free to reopen.