Python Forum
How to clear IPython console in Spyder?
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to clear IPython console in Spyder?
#1
Is there an equivalent in Python for clc command in MATLAB?
I am using Spyder and would like to clear the IPython console from time to time.
Reply
#2
Have you tried Ctrl + L?
Commands that may work !clear, !CLS.
Reply
#3
Thanks.
!cls works well for me(!clear didn't, but that's fine).
May I ask another question?
Is there also equivalent in Python of 'clear all' (or 'clearvars') command in MATLAB?, to erase all variables through a command.
Reply
#4
(Jun-22-2018, 04:12 PM)Vai Wrote: !cls works well for me(!clear didn't, but that's fine).
My guess is that ! just pushes whatever follows it to the underlying shell. In windows, cls clears the screen, while the same thing is clear in bash.

(Jun-22-2018, 04:12 PM)Vai Wrote: to erase all variables through a command.
Why would you want to do that? Just stop using those variables if you don't want to use them, lol.
Reply
#5
I would not recommend it - but that will work.
Output:
In [1]: for name in list(globals()): ...: if name[0] != '_': ...: del globals()[name] ...:
Of course, that most probably will render your session unusable - even with _ guard
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
It would be bit disappointing if there is no command to clear all variables.
I can manually clear them by selecting all in variable explorer and then remove, but would like to do through a command.
Reply
#7
But why? I don't understand why that'd be a relevant thing to want to do. When they leave scope, the garbage collector will dispose of them. And if you're polluting the global namespace... who cares? It's just an interactive session.
Reply
#8
@nilamo & @valcano63
I am, of course, not sure of the intent of @Vai, but clearing the environment at the beginning of a script is commonly used to ensure that the results of the script are perfectly reproducible. It is considered 'Best Practice'.
Your intuition that this question was irrelevant, particularly in the context of an interactive session, seems counter-intuitive, given that @Vai had a (good) reason for asking and that in a non-interactive session the environment would no longer exist after the scripted exited anyway.

@Vai
The code from @volcano63 is approximately equivalent to how this is done in R/R-Studio ("rm(list=ls())").
The link below has a similar code example.
It also states that "%reset" is the right answer, but that prompts for confirmation, which seems to me one step worse than having to click on the 'Reset the namespace' button on the 'Variable explorer' tab.
Also, Spyder's 'Code analysis' considers "%reset", as well as "!CLS", to be "invalid syntax", which seems terribly odd and like a deal-breaker.
https://github.com/spyder-ide/spyder/issues/2563

Actually, the response on Github was just incomplete. "%reset -f" is non-interactive, and, therefore, seemingly the best option.
That issue/CR did result in a configuration option to "Clear all variables before execution", but that breaks scripts that use caching (e.g., for not reloading large data files), so it does not suit my work-flow.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Spyder console zoom in not working? Dionysis 2 392 Feb-06-2024, 03:31 PM
Last Post: paul18fr
  IPython errors for numpy array min/max methods muelaner 1 508 Nov-04-2023, 09:22 PM
Last Post: snippsat
  Can a program execute code in iPython shell and get result? deanhystad 3 1,661 Jun-17-2022, 03:45 AM
Last Post: Larz60+
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,155 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  IPython console vs Spyder script losc 3 2,672 Apr-30-2020, 04:57 AM
Last Post: deanhystad
  How can I create a multiline input in ipython? DataMower 3 4,307 Oct-28-2019, 08:50 PM
Last Post: DataMower
  ipython autocomplete broke indentation! Exsul 6 4,467 Aug-20-2019, 01:29 AM
Last Post: Exsul
  spyder - won't show results in console kbrummert 0 4,226 Jan-20-2019, 01:11 AM
Last Post: kbrummert
  Stopwatch in Python/IPython shell suvadip 3 4,207 Jul-06-2018, 01:51 PM
Last Post: snippsat
  PyEDA-IPython Problem [Graph] embash 5 4,467 Jun-02-2018, 06:40 PM
Last Post: buran

Forum Jump:

User Panel Messages

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