Setupterm could not find terminal, in Python program using curses

大城市里の小女人 提交于 2019-11-26 11:25:02

问题


I am trying to get a simple curses script to run using Python (with PyCharm 2.0).

This is my script:

import curses
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)
while 1:
    c = stdscr.getch()
    if c == ord(\'p\'): print(\"I pressed p\")
    elif c == ord(\'q\'): break

curses.nocbreak(); stdscr.keypad(0); curses.echo()
curses.endwin()

When I run this from my IDE (PyCharm 2) I get the following error:


_curses.error: setupterm: could not find terminal
Process finished with exit code 1

If I run the script from bash it will simply be stuck in the while loop not reacting to either pressing p or q.

Any help would be appreciated.


回答1:


You must set enviroment variables TERM and TERMINFO, like this:

export TERM=linux
export TERMINFO=/etc/terminfo

And, if you device have no this dir (/etc/terminfo), make it, and copy terminfo database.

For "linux", and "pcansi" terminals you can download database:

  • http://forum.xda-developers.com/attachment.php?attachmentid=2134052&d=1374459598
  • http://forum.xda-developers.com/showthread.php?t=552287&page=4



回答2:


Go to run/debug configuration(the one next to Pycharm run button). Sticking on Emulate Terminal In Output Console. Then you will be able to run your program with the run button.




回答3:


I found this question when searching for examples because I am also learning to use curses so I don't know much about it. I know this works though:

import curses
try:
    stdscr = curses.initscr()
    curses.noecho()
    curses.cbreak()
    stdscr.keypad(1)
    while 1:
        c = stdscr.getch()
        if c == ord('p'):
            stdscr.addstr("I pressed p")
        elif c == ord('q'): break
finally:
    curses.nocbreak(); stdscr.keypad(0); curses.echo()
    curses.endwin()

I also added the try: finally: to make sure I get the terminal to it's original appearance even if something simple goes wrong inside the loop.

You have to use the addstr to make sure the text is going to be displayed inside the window.




回答4:


I was having the same problem. See Curses Programming with Python - Starting and ending a curses application.

There's a curses.wrapper() function that simplifies the process of starting/ending a curses application.

Here's the example from the Python doc:

from curses import wrapper

def main(stdscr):
    # Clear screen
    stdscr.clear()

    # This raises ZeroDivisionError when i == 10.
    for i in range(0, 11):
        v = i-10
        stdscr.addstr(i, 0, '10 divided by {} is {}'.format(v, 10/v))

    stdscr.refresh()
    stdscr.getkey()

wrapper(main)



回答5:


If you are using macOS and running PyCharm you will have to set environment variables from the IDE itself, for execution scope.

Edit Configurations -> Environment variables

then add the below name-value pairs

TERM linux

TERMINFO /etc/zsh

The above is equivalent to exporting environment variable from the console which is done like this

$ export TERM=linux
$ export TERMINFO=/bin/zsh

the default for TERM is xterm, other values are [konsole, rxvt] rxvt for example is often built with support for 16 colors. You can try to set TERM to rxvt-16color.

/bin/zsh is path of the terminal application that I use in mac.

It's like telling your program that you will be logging into linux(TERM) like terminal which can be found at /bin/zsh. For using bash shell it could be something like /bin/bash .




回答6:


You'll see this error if you're using Idle. It's because of Idle's default redirection of input/output. Try running your program from the command line. python3 <filename>.py



来源:https://stackoverflow.com/questions/9485699/setupterm-could-not-find-terminal-in-python-program-using-curses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!