Python Forum
Curses could not find terminal
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Curses could not find terminal
#1
I want to start programming with the module curses in Python on my
Raspberry pi3(with rasbian), But I already ran into a problem.

I wrote my first 2 lines of code in IDLE3 and executed them:

import curses
stdscr = curses.initscreen
but I got this output:
Output:
Traceback (most recent call last): File "/home/pi/Desktop/programms/Tools/curse.py", line 3, in <module> curses.initscr() File "/usr/lib/python3.5/curses/__init__.py", line 30, in initscr fd=_sys.__stdout__.fileno()) _curses.error: setupterm: could not find terminal
I already know that there is the way to execute it in the linux terminal,
but it would be just way to much work if I want to test some code every 10 seconds and I have no Idea how to even do that.
If there is no other way, i'll do it with the linux terminal, if someone could
explain it to me (please :) ).
Reply
#2
I don't think there is a way to do this within IDLE3. When your program runs in Idle, its standard streams are pipes that communicate with Idle's main program.

What you could do is launch a terminal from your program's main function, something along the line of:

import curses
import subprocess

def main():
    try:
        scr = curses.initscr()
    except _curses.error:
        subprocess.call(['lxterminal', '--command', 'python', __file__])
    else:
        run_normal_code(scr)
It should be possible to add an option to prevent a fork bomb in case curses.initscr() also fails in the child process. One can use argparse to add an option.

You could also use sys.stdout.fileno() which succeeds in a real terminal but raises io.UnsupportedOperation in IDLE3.
Reply
#3
It kinda does exactly nothing. XD
It doesn't give me an error message but also doesn't open the terminal.
It should do at least something without the name of the file, right?
(this message should be in no way aggressive. Thank you for your Idea.)
Reply
#4
The fact is that I don't yet use Raspberry pi. If you run the command lxterminal in a terminal, does it launch a second terminal? If not, you need to find the command to launch a terminal. This command has an option to start any program within the launched terminal.
Reply
#5
I've cropped the code but now it just goes crazy.
it keeps opening and closing terminals @_@
import curses
import subprocess
subprocess.call(['lxterminal'])
Reply
#6
You have launched a fork bomb. You'd better shutdown and reboot. It's not the three lines you just posted that open and close many terminals. Can you post the real code?

Here is a full code that works for me in linux, but the terminal application is konsole, which has its own options

import argparse
import subprocess
import sys

def main_work(args):
    print('Hello from term-example')

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--noterminal',
        help="don't start a new terminal'",
        action='store_true')
    ns = parser.parse_args()
    try:
        sys.stdout.fileno()
    except Exception:
        # not a terminal for sure
        if ns.noterminal:
            raise RuntimeError('Need a terminal to run.')
        else:
            subprocess.call(['konsole', '--noclose', '-e', sys.executable, __file__, '--noterminal'])
    else:
        main_work(ns)

    
if __name__ == '__main__':
    main()
When I run this code in a terminal, it simply executes the code in main_work(). When I run it from IDLE3 by pressing the F5 key, it launches a new console and executes the code from main_work() in this console.

The konsole command has an option --noclose so that it stays here after the execution of
the code. An alternative is to add a command that waits at the end, such as if ns.noterminal: input()
Reply
#7
Oh, it didnt liked it.
Output:
Traceback (most recent call last): File "C:\Users\Hendrik\Desktop\Programmieren\Python\2\Calculator.py", line 16, in main sys.stdout.fileno() io.UnsupportedOperation: fileno During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\Hendrik\Desktop\Programmieren\Python\2\Calculator.py", line 28, in <module> main() File "C:\Users\Hendrik\Desktop\Programmieren\Python\2\Calculator.py", line 22, in main subprocess.call(['konsole', '--noclose', '-e', sys.executable, __file__, '--noterminal']) File "C:\Users\Hendrik\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 267, in call with Popen(*popenargs, **kwargs) as p: File "C:\Users\Hendrik\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 709, in __init__ restore_signals, start_new_session) File "C:\Users\Hendrik\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 997, in _execute_child startupinfo) FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden
But I have given up by this point.
Thanks for your help, but my system seems too hate curses,
You could say its- "cursed" XD
Reply
#8
The code needs to be adapted for the Raspbery pi by replacing the konsole command by the terminal command that works on your machine. I think it is lxterminal but you need to find the correct way to call it yourself. This has nothing to do with curses.
Reply
#9
I have tried it with lxterminal, but nothing happened.
I tried lxterminal in the terminal, and it worked.
That means it is the right terminal, right?
Reply
#10
I think what may happen is that the lxterminal closes immediately after the program exits. You could try

import argparse
import subprocess
import sys
import traceback
 
def main_work(args):
    print('Hello from term-example')
 
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--noterminal',
        help="don't start a new terminal'",
        action='store_true')
    ns = parser.parse_args()
    try:
        sys.stdout.fileno()
    except Exception:
        # not a terminal for sure
        if ns.noterminal:
            raise RuntimeError('Need a terminal to run.')
        else:
            subprocess.call(['lxterminal', '-e', '{} {} --noterminal'.format(sys.executable, __file__)])
    else:
        try:
            main_work(ns)
        except Exception:
            if ns.noterminal:
                traceback.print_exc()
                input()# <---- this is to prevent the program to end and close the terminal
            else:
                raise
        else:
            if ns.noterminal:
                input() # <---- this is to prevent the program to end and close the terminal
     
if __name__ == '__main__':
    main()
Also you can try to run directly this command in a terminal to see if it says anything (this is not python):

lxterminal -e "python3 Calculator.py --noterminal"
Edit: I installed lxterminal in my kubuntu system and the above code works!
Edit: Support added for holding the terminal if the program fails (with exception).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I want to be able to scroll up in curses to see previous text. Caiden 1 780 Jul-28-2023, 01:15 PM
Last Post: deanhystad
  curses issue otalado 2 2,760 Jun-29-2021, 02:07 PM
Last Post: tmz
  How to make curses.border() use A_BOLD atttribute? pjfarley3 0 4,850 Feb-03-2021, 11:22 PM
Last Post: pjfarley3
  Curses script doesn't work wavic 1 4,168 Jan-08-2021, 09:11 PM
Last Post: wavic
  Why aren't all curses panel functions supported in python curses.panel? pjfarley3 2 2,679 Jul-22-2020, 11:08 PM
Last Post: pjfarley3
  curses library autompav96 2 2,906 Mar-02-2019, 02:12 AM
Last Post: woooee
  curses key codes not working jgrillout 0 3,035 Feb-11-2019, 01:46 AM
Last Post: jgrillout
  Pretty table and curses? MuntyScruntfundle 0 2,904 Oct-16-2018, 10:22 AM
Last Post: MuntyScruntfundle
  curses.initscr doesn't work zaphod424 3 9,822 Feb-28-2018, 12:36 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020