我在 Anaconda 4.3.1 的 Spyder 3.1.4 中编辑了三个 python 2.7 文件
(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
当我在 Spyder 中“runfile”“a1.py”时,创建了一个 TestClass 实例 tt,并且我在 Spyder 的 IPython 控制台中运行以下代码:
tt.getArray()
Out[9]:
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
它工作正常,但在 Spider 中运行file a2.py(一个空文件),并在 Spyder 的 IPython 控制台中重新运行“tt.getArray()”后,出现错误:
tt.getArray() 回溯(最近一次调用最后一次):
文件“”,第 1 行,位于 tt.getArray()
文件“TestClass.py”,第 6 行,在 getArray 中 返回 np.zeros((3,4));
AttributeError:“NoneType”对象没有属性“zeros” numpy 丢失了,根据我的经验,Spyder 中的任何“runfile”运算符都会导致 numpy 丢失。任何有关“tt”的代码都不能写在 a2.py 中,因为运行新文件时,tt 已导入的包丢失了。这是一个错误吗?或者Spyder、Ipython需要进一步配置或设置参数吗?或者spyder中的“runfile”命令需要额外的参数?
我对这个错误感到非常生气,请告诉我哪里出了问题。
最佳答案
由于Spyder具有“用户模块重新加载(UMR)”属性,我们使用“runfile”函数运行脚本,Spyder将重新加载所有用户创建的模块。
在我的环境中,Spyder 重新加载 TestClass 但不重新加载 numpy,并且 TestClass 限制的 numpy 被卸载,因此“runfile”可能会导致用户模块无效。
在 工具->首选项->Python 解释器 我们可以关闭 UMR
关于model - anaconda 和 Spyder 中的包加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44061754/