Forums

Can't get attribute (Pickle, Python, Flask)

Hi,

I'm very new to this. I have a bit of python code that loads a pickle file which contains a Dataframe with class objects. Below I'll show a simplification of my problem

Module.py

class Module: 
    def __init__(self,ID):
        self.ID = ID
#some other functions

Let's say in an earlier function (in the same file) I have created a dataframe looking like this [ID,Module] and pickled it. Now I want to load that pickle file

def loadList():
    moduleList = pickle.load(open('/home/user/mysite/modules.p','rb'))

If I run this I get: AttributeError: Can't get attribute 'Module' on <module 'main' (built-in)>

The code works fine locally but not on PA. Any help is greatly appreciated.

Thanks.

edited: added open() to pickle.load

pickle.load takes a file-like object, not a pair of strings and the fact that your exception isn't complaining about that suggests to me that the exception is not caused by that line. Find where the exception is actually coming from and it will probably be more obvious what the problem is.

Thanks for your quick response. I mistyped. It should be

moduleList = pickle.load(open('/home/user/mysite/modules.p','rb'))

And the error occurs on the line where I call

loadList()

Perhaps you need the Module class to be defined and you don't?