spyder或IPython中奇怪的模块重新加载行为 [英] Strange module reload behavior in spyder or IPython

查看:168
本文介绍了spyder或IPython中奇怪的模块重新加载行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在重新运行spyder中的脚本时遇到问题,该脚本会动态列出其自身的某些属性.

I have an issue re-running a script in spyder, which dynamically tabulates some of its own attributes.

这是一个代表我在做什么的最小例子.我有一个使用常规运行(F5)命令运行的源脚本.它在它所在的目录中运行:

Here is a minimal example that is representative of what I am doing. I have a source script that I run with the normal Run (F5) command. It runs in the same directory that it lives in:

runfile('C:/some/path/test.py', wdir='C:/some/path')

test.py

import sys

def x():
    pass

def y():
    pass

x.add = y.add = True

if __name__ == '__main__':
    a = [obj for obj in tuple(sys.modules[__name__].__dict__.values())
                if getattr(obj, 'add', False)]
    print(a)

我可以重新运行此脚本几次,并始终获得相同的预期结果:

I can re-run this script a couple of times, and always get the same expected result:

[<function x at 0x0000025E793DBD90>, <function y at 0x0000025E793DB598>]

我希望,如果我将其中一个功能的名称从x更改为f,并将属性分配为f.add = y.add = True,我将得到相同的结果,但是更改了xf.相反,我得到了

I would expect that if I change the name of one of the functions, say from x to f, and the attribute assignment to f.add = y.add = True, I would get the same result, but with x changed to f. Instead, I get

[<function x at 0x0000025E793DB510>, <function y at 0x0000025E793DBBF8>, <function f at 0x0000025E793DBA60>]

鉴于新功能f出现在列表中,但旧的x仍然存在,我认为该脚本的模块缓存未正确清除.

Given that the new function f shows up in the list, but the old x is still there as well, I think that the module cache for the script is not being cleared properly.

我一直在使用控制台上的橡皮擦按钮手动清除工作区,但是我觉得必须有更好的解决方案,或者至少是对正在发生的事情的解释.也许这是UMR的问题?

I have been manually clearing the workspace using the eraser button over the console, but I feel like there must be a better solution, or at least an explanation of what is happening. Perhaps it is an issue with UMR?

我正在使用以下命令运行Spyder 3.3.3:Python 3.7.3 64位| Qt 5.9.6 | PyQt5 5.9.2 | Windows 10,IPython 7.4.9

I am running Spyder 3.3.3 with the following: Python 3.7.3 64-bit | Qt 5.9.6 | PyQt5 5.9.2 | Windows 10, IPython 7.4.9

推荐答案

这是Spyder中的一项功能,旨在让您从停止点继续运行,尤其是在考虑更长的运行时间以生成特定结果时,以及变量浏览器功能.

This is a feature in Spyder that is built on purpose to allow you to continue running from stop points, especially when considering longer run times to generate specific results, and also powers the variable explorer function.

尽管可以按照此答案中的说明进行操作,但可以在每次运行前清除所有变量或重新启动.

There is a way around it though following the instructions in this answer to Clear all variables before each run or by restarting.

但是,正如 roganjosh 所指出的那样,清除名称空间并不一定更好.

However, as pointed out by roganjosh, clearing the namespace is not necessarily better.

您可以运行一个耗时10分钟的函数来处理数据并将其返回为全局名称,然后只需对该函数进行哈希就可以在以后的每次运行中调用该函数,而不必再花费处理时间(直到您擦除名称空间,是).

You can run a function that takes 10 mins to process data and return it to a global name, then just hash that function call out for every subsequent run and never have to incur the processing time again (until you wipe the namespace, that is).

如果您只想重置一个或一组变量,则可以使用重置选择性

If you want to only reset one or a group of variables, you can use reset_selective

%reset_selective [-f] regex

这篇关于spyder或IPython中奇怪的模块重新加载行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆