Setupterm could not find terminal, in Python program using curses

Each Answer to this Q is separated by one/two green lines.

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()
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()

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.

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:

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.

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

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
    stdscr = curses.initscr()
    while 1:
        c = stdscr.getch()
        if c == ord('p'):
            stdscr.addstr("I pressed p")
        elif c == ord('q'): break
    curses.nocbreak(); stdscr.keypad(0); curses.echo()

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.

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

    # 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))



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 .

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .