【问题标题】:Package load error in anaconda and Spyderanaconda 和 Spyder 中的包加载错误
【发布时间】:2017-10-19 02:15:53
【问题描述】:

我在 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 中“运行文件”“a1.py”时,会创建一个 TestClass 实例 tt,然后我在 Spyder 的 IPython 控制台中运行以下代码:

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

它可以正常工作,但是在我在 Spider 中运行文件 a2.py(一个空文件)并在 Spyder 的 IPython 控制台中重新运行“tt.getArray()”之后,出现了错误:

tt.getArray() Traceback(最近一次调用最后一次):

文件“”,第 1 行,在 tt.getArray()

文件“TestClass.py”,第 6 行,在 getArray 中 返回 np.zeros((3,4));

AttributeError: 'NoneType' 对象没有属性 'zeros' numpy 丢失了,根据我的经验,Spyder 中的任何“运行文件”运算符都会导致丢失 numpy。任何关于“tt”的代码都不能写在 a2.py 中,因为 tt alread 导入的包在运行新文件时丢失。这是一个错误吗?还是 Spyder、Ipython 需要进一步配置或设置参数?还是spyder中的“runfile”命令需要额外的参数?

我被这个错误弄疯了,请告诉我哪里出错了。

【问题讨论】:

    标签: model load ipython anaconda spyder


    【解决方案1】:

    因为Spyder有“用户模块重载(UMR)”属性,我们用“runfile”函数运行一个脚本,Spyder会重新加载所有用户创建的模块。

    在我的环境中,Spyder 重新加载 TestClass 而不是重新加载 numpy,并且 TestClass bounded numpy 被卸载,所以“runfile”会导致用户模块无效。

    在 工具->首选项->Python解释器 我们可以关闭UMR

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 2018-05-05
      • 2021-02-27
      • 2014-12-04
      • 2020-12-30
      • 2019-04-27
      • 1970-01-01
      相关资源
      最近更新 更多