42

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.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
user1017102
  • 787
  • 1
  • 7
  • 14

6 Answers6

39

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:

ohhorob
  • 11,695
  • 7
  • 41
  • 50
irk_coder
  • 399
  • 3
  • 3
  • i ran into this running a script from cron, where you'll need to remove 'export' on each line to set env vars without crontab complaining – baku Apr 15 '20 at 20:21
  • It's unlikely that `TERM=linux` is correct, and setting `TERMINFO` is redundant or conflicting. The value to be used for `TERM` depends on the terminal which is used for display (IDE developers have apparently overlooked doing this properly). – Thomas Dickey Aug 20 '23 at 08:10
30

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.

Trieu Nguyen
  • 933
  • 1
  • 11
  • 17
2

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

Clarius
  • 1,183
  • 10
  • 10
1

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.

rui
  • 159
  • 6
0

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)
Zachary Wilson
  • 1,244
  • 1
  • 8
  • 4
-1

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 .

Community
  • 1
  • 1
hassan_ashraf
  • 305
  • 1
  • 2
  • 7
  • zsh and bash are not your terminal applications. They are shells that can accept input and produce output to a terminal, just like any other program, such as grep, sed, VS Code or Google Chrome. An example of a terminal application would be xterm, Konsole or GNOME Terminal. TERMINFO should not be your terminal application. It should be the directory containing terminal database files. E.g. /usr/share/terminfo/ – Larry Aug 05 '22 at 00:30