29

Is there a way to clear the "Run" console in PyCharm? I want a code that delete/hide all the print() made previously. Like the "clear_all" button, but without having to press it manually.

I have read that there is a way to do it in a terminal with os.system("cls"), but in PyCharm, it only adds a small square without clearing anything.

Also, I don't want to use print("\n" *100) since I don't want to be able to scroll back and see the previous prints.

Simon Peloquin
  • 288
  • 1
  • 3
  • 7
  • https://www.jetbrains.com/pycharm/webhelp/run-tool-window.html#d595386e316 – rnrneverdies Dec 02 '14 at 03:28
  • 1
    But is there a way to program something like the "clear_all" button? – Simon Peloquin Dec 02 '14 at 03:39
  • "it only add a small square without clearing anything." terminal issue? – rnrneverdies Dec 02 '14 at 03:41
  • I'm not using a terminal here. I'm executing the code in PyCharm directly and I want to clear all the prints made previously. – Simon Peloquin Dec 02 '14 at 03:51
  • 1
    After searching around a bit, I understand the question as : is there a way for Python to interact with Pycharm's `Run` console (which I've started to believe is not a terminal but more like a simple display screen). Which may be overly complicated. Depending on how far you're willing to go, you can always look for that button's implementation in Pycharm CE code. – Arnaud P Dec 02 '14 at 09:47
  • Although there's little to no hope the objects there will be available to your Python process.. – Arnaud P Dec 02 '14 at 09:55

14 Answers14

29

In Pycharm:

  1. CMD + , (or Pycharm preferences);
  2. Search: "clear all";
  3. Double click -> Add keyboard shortcut (set it to CTRL + L or anything)
  4. Enjoy this new hot key in your Pycharm console!
Taylan Aydinli
  • 4,333
  • 15
  • 39
  • 33
You Shengwei
  • 391
  • 1
  • 3
  • 3
13

Pycharm Community Edition 2020.1.3

You can right click anywhere above the current line on the console, and choose the "Clear All" option. It'll clear the console

enter image description here

baldr
  • 2,891
  • 11
  • 43
  • 61
  • 1
    Maybe my question was not clear enough. But I wanted to have that same effect without interacting manually. Is there a way to do this with code? – Simon Peloquin Jul 16 '20 at 19:45
11

How to

  1. Download this package https://github.com/asweigart/pyautogui. It allows python to send key strokes.

You may have to install some other packages first

If you are installing PyAutoGUI from PyPI using pip:

Windows has no dependencies. The Win32 extensions do not need to be installed.

OS X needs the pyobjc-core and pyobjc module installed (in that order).

Linux needs the python3-xlib (or python-xlib for Python 2) module installed. Pillow needs to be installed, and on Linux you may need to install additional libraries to make sure Pillow's PNG/JPEG works correctly. See:

  1. Set a keyboard shortcut for clearing the run window in pycharm as explained by Taylan Aydinli
  1. CMD + , (or Pycharm preferences);
  1. Search: "clear all"; Double click ->
  1. Add keyboard shortcut (set it to CTRL + L or anything)
  1. Enjoy this new hot key in your Pycharm console!
  1. Then if you set the keyboard shortcut for 'clear all' to Command + L use this in your python script

     import pyautogui
     pyautogui.hotkey('command', 'l')
    

Example program

This will clear the screen after the user types an input.

If you aren't focused on the tool window then your clear hot-key won't work, you can see this for yourself if you try pressing your hot-key while focused on, say, the editor, you won't clear the embedded terminals contents.

PyAutoGUI has no way of focusing on windows directly, to solve this you can try to find the coordinate where the run terminal is located and then send a left click to focus, if you don't already know the coordinates where you can click your mouse you can find it out with the following code:

import pyautogui
from time import sleep
sleep(2)
print(pyautogui.position())

An example of output:

(2799, 575)

and now the actual code:

import pyautogui

while True:
    input_1 = input("?")
    print(input_1)
    pyautogui.click(x=2799, y=575)
    pyautogui.hotkey('command', 'l')
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jonathan De Wet
  • 336
  • 1
  • 4
  • 12
7

Easy Method: Shortcut: Control K, Right click on terminal and clear Buffer

5

There's also another way of doing it using the system class from os. All you need to do is have this code:

from os import system, name


# define our clear function
def clear():
    # for windows the name is 'nt'
    if name == 'nt':
        _ = system('cls')

    # and for mac and linux, the os.name is 'posix'
    else:
        _ = system('clear')

# Then, whenever you want to clear the screen, just use this clear function as:
clear()

However, in order for this functionality to work in pycharm, you need to enable "Emulate terminal in output console". You can find this when you right-click the file you want to add this function to and select "modify run configuration", then it's under Execution option. Here's a screenshot: pycharm screensho

vayana
  • 3
  • 2
Mahak Khurmi
  • 51
  • 1
  • 1
  • To the best of my knowledge, with all contemporary versions of Windows, Python's os.system('clear') works fine, so no need for creating a function. Also, if you'd like to make this a default for all projects, you can edit the configuration template for Python, rather than the configuration specific to a program or file. You'll find the link in the lower left corner of the settings screen previously posted. – Dr. C. Aug 14 '23 at 02:39
  • I stand corrected. I am accustomed to a module that tends to the clear vs cls issue. The line in the module that reconciles the two is: ```cls = 'cls' if 'nt' in os.name else 'clear'``` Thereafter, simply call ```os.system(cls)``` (no quotes). – Dr. C. Aug 14 '23 at 03:21
2

You could just do a ("\n" * 100000000), so it'll be impossible to scroll back.

Iconman
  • 63
  • 8
2

Here's the solution to the OP's question/dilemma:

To enable os.system('clear') for all projects in PyCharm's Run pane, click 'Edit Configurations' in the 'Run Configuration' drop-down menu.

In the lower left of the 'Run/Debug Configurations' window, click 'Edit configuration templates' to launch the 'Run/Debug Configuration Templates' editor. Select 'Python' on the left, and then scroll down to the 'Execution' section and select 'Emulate terminal in output console.' Then, click OK on each settings dialog to close it.

Here's a screenshot of the dialogs (PyCharm 2023.1.3):

Dr. C.
  • 76
  • 6
1

I just relised that instead of going to the trouble of setting up a shortcut, you could just set up a command using PyAutoGUI to click on the trash bin on the side of the window e.g

note, to install pyautogui click on the end of the import pyautogui line, then press alt+enter and click install pyautogui.

import pyautogui

# to find the coordinates of the bin...
from time import sleep
sleep(2) # hover your mouse over bin in this time
mousepos = pyautogui.position() gets current pos of mouse
x,y = mousepos # storing mouse position 
print(mousepos) # prints current pos of mouse

# then to clear it;
pyautogui.click(x, y) # and just put this line of code wherever you want to clear it

(this isn't perfect thanks to the time it takes to run the code and using the mouse, but it is reasonable solution depending on what you are using it for.) I hope this answer is helpful even though this is an old question.

Darkstar Dream
  • 1,649
  • 1
  • 12
  • 23
1

Sorry to say this, here the main question is how to do it programmatically means while my code is running I want my code to clear previous data and at some stage and then continue running the code. It should work like reset button.

After spending some time on research I solved my problem using Mahak Khurmi's solution https://stackoverflow.com/a/67543234/16878188.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • 1
    Please don't add "thank you" as an answer. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation), you will be able to [vote up questions and answers](https://stackoverflow.com/help/privileges/vote-up) that you found helpful. - [From Review](/review/late-answers/29791090) – Paulo Sep 10 '21 at 12:19
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 10 '21 at 14:00
0

In PyCharm terminal you can type 'cls' just like in linux terminal.

For Python Console (where you see the output) assign a shortkey for "clear all" in File -> Settings -> Keymap -> Other -> "Clear all"

You can also click somewhere on the PythonConsole -> Right button -> clear.

Hope it helps

0

If you edit the run configuration you can enable "emulate terminal in output console" and you can use the os.system("cls") line and it will work normally.

S.B
  • 13,077
  • 10
  • 22
  • 49
0

Iconman had the easiest answer.

But simply printing "\n" * 20 (or whatever your terminal height is) will clear the screen, and the only difference is that the cursor is at the bottom.

I came here because I wanted to visually see how long each step of a complex process was taking (I'm implementing a progress bar), and the terminal is already full of scrolling logging information.

I ended up printing ("A" * 40) * 20, and then "B" and "C" etc., and then filming it. Reviewing the video made it easy to see how many seconds each step took. Yes I know I could use time-stamps, but this was fun!

-1

Just click the trash can icon to the left of the command window and it clears the command history!

slehar
  • 319
  • 3
  • 6
-1

In PyCharm 2019.3.3 you can right click and select "Clear All" button.This is deleting all written data inside of the console and unfortunately this is manual.

sinan
  • 31
  • 3