'Clearing user-created variables in Python
I am an absolute beginner in Python. I have been using R and Matlab for data analysis for quite some time. One of the great things about these languages (or tools) is that I could run clear all
in Matlab or rm(list=ls())
in R to clear the variables I created. I could then continue to experiment with my snippet of code.
I am struggling to find such a solution in Python. I researched Is there a way to delete created variables, functions, etc from the memory of the interpreter? and found that there are two ways to accomplish what I am trying to do, but in reality they are not any close.
First method:
%Reset
This command deletes all variables and imported setting in PyCharm to run interactive mode. That is, after running above command, I have to re-run
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
This is very annoying.
Second Method:
Another suggested method is to write a for
loop and ignore functions starting with __
. This also doesn't work for the reasons above. For instance, when I start my PyCharm, I run
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
to ensure that I am able to run a bunch of commands together just as I would do in Matlab and R (In PyCharm, the keyboard shortcut is [Alt + Shift + E; This is called as Execute Selection in Console]). I have noticed that the for
loop mentioned in that Stackoverflow thread deletes some of the variables created by running above two lines of code. This means that I won't be able to execute statements by selecting them at a time. I would have to type them individually on the prompt, which is annoying. Another option would be to run above two lines of code every time I execute the for
loop, which is annoying as well.
As an aside, I did some research to understand what happens after running above two lines of code. I ran dir()
after running above two lines to get a list of all variables existing in my session.
The list looks like this
Out[4]:
['In',
'InteractiveShell',
'Out',
'_',
'_2',
'__',
'___',
......(continued)
'sys']
Is there any way, I can delete user-created variables without deleting above variables? In effect, I want to replicate what rm(list=ls())
would do for me in R.
I see that there are a number of people who have asked this question outside StackOverflow such as https://github.com/spyder-ide/spyder/issues/2563.
Thanks for any help. I am using PyCharm with the latest Conda distribution (3.6).
Solution 1:[1]
Try this:
>>> x = 1
>>> x
1
>>> globals().clear()
>>> x
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
x
NameError: name 'x' is not defined
Note that the the docs don't actually say that you can safely modify the dictionary that globals()
returns, nor that modifying this dictionary will have the effect you intend (or any effect at all). But if you're just doing it for convenience in the REPL, then if it works, it works.
Solution 2:[2]
If you wish to simply clear all variables you've defined in the console.. That is to undo everything your code has done and 'start fresh'..
Type:
exit
in the console
In Spyder this simply clears the workspace of anything your code has done. The same effect as closing and restarting Anaconda, but without having to do so. Now you can run your code with a fresh workspace!
Solution 3:[3]
How about running the code in "Interactive mode" in VSC? You can close the interactive window to erase all the value of the variables.
Solution 4:[4]
Using PyCharm 2021.3.3 (Community Edition) built March 16. 2022: There are two columns of buttons on the left of the console the bottom left one says "New Console" on hover. Pressing it gives a new console (duh) with no user variables.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | Anil Kamat |
Solution 4 | Steve Maguire |