Package load error in anaconda and Spyder

2019-07-14 11:50发布

问题:

I have three python 2.7 files edit in Spyder 3.1.4 in Anaconda 4.3.1

(1). TestClass.py :Just define a class 
import numpy as np
class TestClass:
    def getArray(self):
        return np.zeros((3,4));
(2). a1.py
from TestClass import *;
tt=TestClass();

(3). a2.py
#just a empty python file

When I "runfile" "a1.py" in Spyder, a TestClass instance tt was created, and I run following code in Spyder's IPython console:

tt.getArray()
Out[9]: 
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])

It works correctly, but after I runfile a2.py (a empty file) in Spider, and I re-run "tt.getArray()" in Spyder's IPython console, the error occours:

tt.getArray() Traceback (most recent call last):

File "", line 1, in tt.getArray()

File "TestClass.py", line 6, in getArray return np.zeros((3,4));

AttributeError: 'NoneType' object has no attribute 'zeros' the numpy became missing, in my experience any "runfile" operator in Spyder will lead to missing numpy. Any code about "tt" can't not write in a2.py because tt alread imported package are missing when run a new file. Is this a bug ? Or Spyder, Ipython need further configure or set parameter? Or "runfile" command in spyder need additional paramter?

I have go mad with this error, please tell me where I went wrong.

回答1:

Because Spyder have "User Module Reloade (UMR)" Property, we us "runfile" function run a script, Spyder will reload all the user created modules.

In my environment, Spyder reload TestClass but not reload numpy, and TestClass bounded numpy was unloaded, so "runfile" can cause users modules invalid.

In Tools->Preferences->Python Interpreter we can close UMR