Issue
I have an issue re-running a script in spyder, which dynamically tabulates some of its own attributes.
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>]
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>]
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.
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?
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
Solution
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.
However, as pointed out by roganjosh, clearing the namespace is not necessarily better.
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
Answered By - MyNameIsCaleb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.