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).