Reputation: 547
I know there are many other questions asking the same thing, but none of them work for me, and I can't comment asking for help. They just don't work for me. I have tried all sorts of things! I'm on a Mac, and using Pycharm with what I think is the most recent Python (v. 3), but I'm not sure.
1) Cmd + L does not work.
2)
import os
clear = lambda: os.system('clear')
clear()
well = input("The screen is not clear.")
says "TERM environment variable not set."
3)
absolutely_unused_variable = os.system("clear")
says the same TERM thing.
4)This
import subprocess, platform
if platform.system()=="Windows":
subprocess.Popen("cls", shell=True).communicate()
else: #Linux and Mac
print("\033c", end="")
prints a c for me in the final line.
What am I doing wrong? I tried all of the above, using all possible combinations of clear and cls. To be sure it wasn't my Pycharm I tried all on IDLE Python 3.7.2. I outright refuse to \n 100 times. Any suggestions? :)
Upvotes: 0
Views: 2884
Reputation: 143
In Unix-like systems a console window is a character device that can be anything from a GUI window an actual terminal. Different types of "devices" require different methods to clear the screen. Normally when a shell is started in a terminal the TERM environment variable is set to a value that indicates what type of terminal it is, and routines that clear the screen consult this in order to determine what characters to write in order to clear it.
If you try your first solution in a Mac terminal window, it will clear the screen. If you try it without even using Python, but unset the TERMINAL environment variable, then not even the system 'clear' command will know how to clear the screen:
$ echo $TERM
xterm-256color
$ unset TERM
$ clear
TERM environment variable not set.
The problem you have is not specific to Python, but rather to the PyCharm terminal window, which does not appear to set this environment variable.
It may be that the PyCharm terminal window does not support ANSI or other sequences for clearing the screen. It does appear to support some escape sequences for colored text, at least. It may be that it just doesn't set the environment variable correctly. You could try export TERM=ansi
in the terminal, and then try to run your clear command to see if that works.
Another thing you might try is using Click to clear the screen in a device- and platform-independent way. Click is pretty great in general. The following, for example, works for me even after I unset TERM in a terminal:
import click
click.clear()
Upvotes: 2
Reputation: 6612
I believe this topic is more related to PyCharm, you can create your own keyboard shortcuts in PyCharm for clearing the screen as if it were on console or cmd:
Upvotes: 0