2

I have just migrated from Jupyter to Spyder. On the left of the screen there is a console that shows the output. When I rerun the code it keeps adding new lines. I want to start with a new screen. How?

PS1: When I go to the IPyhton console and write cls or clear, it works. But I want to do that from Python code. The latter doesn't recognize that command, but IPython console does!

PS2: I've tried

import os 
os.system('cls')  # on windows

It didn't work. It shows a CMD window for a moment and vanishes. But the console window in Spyder still shows the old errors, besides the new ones.

Alex Deft
  • 2,531
  • 1
  • 19
  • 34
  • PS3: I can run the code after restarting the kernel. That's an obvious solution but I want something quick. – Alex Deft Apr 20 '19 at 08:42
  • Possible duplicate of [Clearing the screen in IPython](https://stackoverflow.com/questions/6892191/clearing-the-screen-in-ipython) – Tiger-222 Apr 20 '19 at 08:48
  • I've checked question. Didn't work. For what's worth, I'm running Spyder via Anaconda, and I have Windows. – Alex Deft Apr 20 '19 at 08:52
  • use `!cls` and change the interpreter to `ipython` instead of the standard python interpreter `python` –  Apr 20 '19 at 09:17
  • @IamNotaMathematician Although I don't know how to do that, but you have correctly explained what's happening. Thanks, I will look up how to change the interpreter in Spyder – Alex Deft Apr 20 '19 at 10:09

2 Answers2

2

Windows Spyder 4, Python 3.7:

  • !cls
  • cls
  • cls()
  • Ctrl + L
  • clear
  • clear()
  • print(chr(27) + "[2J")
  • print("\x1b[2J")
  • clear-screen

Windows Terminal, Python 3.9:

  • !cls
  • cls
  • Ctrl + L
  • cls()
  • print(chr(27) + "[2J")
  • print("\x1b[2J")

Linux Terminal, Python 3.8:

  • Ctrl + L
  • clear
Szczerski
  • 839
  • 11
  • 11
1

(Spyder maintainer here) There are two ways to get what you want:

  1. You can go to the menu Run > Configuration per file and select the option called Execute in a dedicated console. That option will clean your console after every execution.
  2. Add the following code to your file:

    from IPython import get_ipython
    get_ipython.run_line_magic('clear', '')
    
Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124