Skip to content
Sign upLog in
← Back to Community
Clearing the Screen in the Classroom IDE
Profile icon
LIAMSARETT

Hi, I'm new to Python as I am working on this project for a class. It is a text-based game and I want the screen to clear after each input but the clear I've imported only works in a separate file that I've attached here. When I copy and paste my code into the classroom project to turn it in, it gives me this error:
"_test_runner.py
TERM environment variable not set."
What can I do to fix this?

Answered by RomeroSchwarz [earned 5 cycles]
View Answer
Voters
Profile icon
LIAMSARETT
Comments
hotnewtop
Profile icon
RomeroSchwarz

Using os.system is generally a bad practice because of the possible exploits with it. I usually use the ANSI escape codes to clear console. This code should solve it:

print("\x1b[2J\x1b[H")

The above code uses two ANSI codes, \x1b[2J to clear console, and \x1b[H to reset cursor position to 1,1.

If this post helped, mark it as the answer

Profile icon
eidhernan

@RomeroSchwarz
Me too. I normally use the Blessed library, which bundles these ANSI escape codes into functions, and also it comes with keypress detection

Profile icon
LIAMSARETT

@RomeroSchwarz
Thank You! These ANSI codes are really useful, although it still doesn't work in the classroom area/IDE, but nothing seems to there. The codes work great in any other situation though.