[Tutor] Unpickling problem

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 17 Jan 2001 12:01:38 -0800 (PST)


On Thu, 18 Jan 2001, Phil Bertram wrote:

> Hi all,
> 
> I have some code in a module named NPCObjects, describing 'Players',
> 'Matches' and collections of them. This code works fine in command line
> mode. However I am trying to write a user interface using Tkinter.
> When try to contruct my NPC object from a menu in Tkinter I get the
> following error.
> Note that the __init__ method of my NPC object unpickles a file to create
> the object.
> 
> What is going wrong ?

You'll probably need to first import those classes before doing the
unpickling --- otherwise, Python won't know what you mean by Players or
Matches.  The documentation gives some explanation on this:

    http://python.org/doc/current/lib/module-pickle.html

"First of all, the class must be defined at the top level in a module."

is the line that's important --- you'll need to make sure that you've done
something like:

    from NPCObjects import Players, Matches

before de-pickling.  Because you were in the interpreter, those classes
were probably toplevel, but in your source code, you may need to add the
above line to make things work again.  Hope this helps!