当我在Spyder(Python 3.6.5)上读取我的.pkl文件时,我遇到了以下错误:
IN: with open(file, "rb") as f:
data = pickle.load(f)
Traceback (most recent call last):
File "<ipython-input-5-d9796b902b88>", line 2, in <module>
data = pickle.load(f)
AttributeError: Can't get attribute 'Signal' on <module '__main__' from 'C:\\Python36\\lib\\site-packages\\spyder\\utils\\ipython\\start_kernel.py'>
背景:
我的程序由一个文件:program.py
组成。在程序中,定义了一个名为Signal
的类以及许多函数。以下是该程序的简化概述:
import numpy as np
import _pickle as pickle
import os
# The unique class
class Signal:
def __init__(self, fq, t0, tf):
self.fq = fq
self.t0 = t0
self.tf = tf
self.timeline = np.round(np.arange(t0, tf, 1/fq*1000), 3)
# The functions
def write_file(data, folder_path, file_name):
with open(join(folder_path, file_name), "wb") as output:
pickle.dump(data, output, -1)
def read_file(folder_path, file_name):
with open(join(folder_path, file_name), "rb") as input:
data= pickle.load(input)
return data
def compute_data(# parameters):
# do stuff
compute_data
函数将返回一个元组列表,格式如下:
data = [((Signal_1_1, Signal_1_2, ...), val 1), ((Signal_2_1, Signal_2_2, ...), val 2)...]
当然,Signal_i_k是一个对象Signal。这个列表将以.pkl格式保存。此外,我正在使用不同参数进行大量迭代的compute_data函数。许多迭代将使用过去计算的数据作为起点,因此将读取相应和所需的.pkl文件。
最后,我同时使用几台计算机,在本地网络上保存计算出的数据。因此,每台计算机都可以访问其他计算机生成的数据,并将其用作起点。
回到错误:
我的主要问题是,当我通过双击文件或通过Windows cmd或PowerShell启动程序时,从未出现过这个错误,程序没有崩溃并且运行时没有明显的问题。
然而,我无法在Spyder中读取.pkl文件。每次尝试时,都会抛出错误。
有什么想法为什么会出现这种奇怪的行为吗?
谢谢!
py -3.6 -m pip install spyder
。(两个工作站的情况略有不同,还安装了Python 2.7)。 - Mathieuimport sys
print(sys.version)
可以让你得到解释器所使用的精确版本。 - IonicSolutions3.6.5 (v3.6.5:f59c0932b4, 2018年3月28日,17:00:18) [MSC v.1900 64位(AMD64)]
- Mathieu