Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setupterm could not find terminal, in Python program using curses

Tags:

python

curses

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.

like image 702
user1017102 Avatar asked Feb 28 '12 16:02

user1017102


4 Answers

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
like image 63
irk_coder Avatar answered Sep 22 '22 13:09

irk_coder


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.

like image 38
Trieu Nguyen Avatar answered Sep 21 '22 13:09

Trieu Nguyen


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

like image 2
Clarius Avatar answered Sep 21 '22 13:09

Clarius


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.

like image 1
rui Avatar answered Sep 24 '22 13:09

rui