Python Forum

Full Version: How to clear IPython console in Spyder?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
Have you tried Ctrl + L?
Commands that may work !clear, !CLS.
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.
(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.
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
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.
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.
@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.