简体   繁体   English

spyder 或 IPython 中奇怪的模块重新加载行为

[英]Strange module reload behavior in spyder or IPython

I have an issue re-running a script in spyder, which dynamically tabulates some of its own attributes.我在 spyder 中重新运行脚本时遇到问题,该脚本会动态地将其自身的一些属性制成表格。

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.我有一个使用正常运行 (F5) 命令运行的源脚本。 It runs in the same directory that it lives in:它在它所在的同一目录中运行:

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

test.py测试.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>]

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 .我希望如果我更改其中一个函数的名称,例如从x更改为f ,并将属性分配给f.add = y.add = True ,我会得到相同的结果,但将x更改为f Instead, I get相反,我得到

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

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.鉴于新的 function f出现在列表中,但旧的x仍然存在,我认为脚本的模块缓存没有被正确清除。

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?也许这是UMR的问题?

I am running Spyder 3.3.3 with the following: Python 3.7.3 64-bit |我正在运行具有以下内容的 Spyder 3.3.3:Python 3.7.3 64 位 | Qt 5.9.6 | Qt 5.9.6 | PyQt5 5.9.2 | PyQt5 5.9.2 | Windows 10, IPython 7.4.9 Windows 10,IPython 7.4.9

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.这是 Spyder 中的一个功能,旨在让您从停止点继续运行,特别是在考虑更长的运行时间以生成特定结果时,并且还为变量资源管理器 function 提供动力。

There is a way around it though following the instructions in this answer to Clear all variables before each run or by restarting.尽管按照此答案中的说明在每次运行之前或通过重新启动清除所有变量,但有一种解决方法。

However, as pointed out by roganjosh , clearing the namespace is not necessarily better.但是,正如roganjosh所指出的,清除命名空间并不一定更好。

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

%reset_selective [-f] regex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM