Python spyder - 清除变量资源管理器以及内存中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45853595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:19:50  来源:igfitidea点击:

spyder - clear variable explorer along with variables from memory

pythonspyder

提问by dataLeo

To clear the console we can use the following command -

要清除控制台,我们可以使用以下命令 -

import subprocess as sp
tmp = sp.call('cls',shell=True)

However, to remove a variable from memory, we often rely upon -

但是,要从内存中删除变量,我们经常依赖 -

  • using delcommand
  • removing variable manually by using the drop-down menu in variable explorer
  • 使用del命令
  • 使用变量资源管理器中的下拉菜单手动删除变量

But both of them are variable specific and hence time-consuming. So is there any general command (like clearunder MATLAB) to remove a variable from memory and thereafter from Spyder's Variable Explorer.

但它们都是变量特定的,因此很耗时。那么是否有任何通用命令(如clear在 MATLAB 下)从内存中删除变量,然后从 Spyder 的变量资源管理器中删除。

回答by NKoder

Go to the IPython console in the Spyder IDE and type %reset. It will prompt you to enter (y/n) as the variables once deleted cannot be retrieved. Type 'y' and hit enter. That's it.

转到 Spyder IDE 中的 IPython 控制台并键入 %reset。它会提示您输入 (y/n),因为一旦删除的变量将无法检索。输入“y”并按回车键。就是这样。

回答by Karan Kaw

In Spyder, Do following steps
Run
Configuration per file...
Clear all variables before execution [Select Checkbox]

This actually clears variables from previous run of the file. Hope it helps.

在 Spyder 中,执行以下步骤
运行
每个文件的配置...
执行前清除所有变量 [选择复选框]

这实际上清除了先前运行文件的变量。希望能帮助到你。

enter image description here

在此处输入图片说明

回答by Leos313

Surfing on the web, I found a hack to solve the annoying problem of clearing the variable explorer every time you want to execute again a script:

在网上冲浪,我发现了一个 hack 来解决每次要再次执行脚本时清除变量资源管理器的烦人问题:

def clear_all():
    """Clears all the variables from the workspace of the spyder application."""
    gl = globals().copy()
    for var in gl:
        if var[0] == '_': continue
        if 'func' in str(globals()[var]): continue
        if 'module' in str(globals()[var]): continue

        del globals()[var]
if __name__ == "__main__":
    clear_all()
    # insert here your code

Basically, it consists of executing the function clear_all()just before everything else. It is writing by yourself the same Matlab's function. Herethe link to the git issue where the solution was proposed.

基本上,它包括在执行clear_all()其他所有操作之前执行该函数。它是自己编写相同的Matlab 函数。 这里是提出解决方案的 git 问题的链接。

回答by Matteo Zambra

As explained in the answerprovided by Karan Kaw, there is a setting to delete all the variables, regardless of the script you are workin on.

正如Karan Kaw提供的答案中所解释的那样,有一个设置可以删除所有变量,而不管您正在使用什么脚本。

Do the following (from the drop-downs bar)

执行以下操作(从下拉栏中)

> Tools> Preferences> Run

> Tools> Preferences> Run

and check the Remove all the variables before executioncheckbox in the General Settingsection. In doing this I use Spyder 3.3.4.

并选中Remove all the variables before executionGeneral Setting部分中的复选框。为此,我使用 Spyder 3.3.4。

This completion may be in order if you want all the scripts you work on to be executed with the preventive deletion of all the variables. Hope it helps

如果您希望在预防性删除所有变量的情况下执行您处理的所有脚本,则此完成可能是合适的。希望能帮助到你