Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6748543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:34:40+00:00 2026-05-26T12:34:40+00:00

I got an error when I use pickle with unittest. I wrote 3 program

  • 0

I got an error when I use pickle with unittest.

I wrote 3 program files:

  1. for a class to be pickled,
  2. for a class which use class in #1,
  3. unittest for testing class in #2.

and the real codes are as follows respectively.

#1. ClassToPickle.py

import pickle
class ClassToPickle(object):
    def __init__(self, x):
        self.x = x
if __name__=="__main__":
    p = ClassToPickle(10)
    pickle.dump(p, open('10.pickle', 'w'))

#2. SomeClass.py

from ClassToPickle import ClassToPickle
import pickle

class SomeClass(object):
    def __init__(self):
        self.pickle = pickle.load(open("10.pickle", 'r'))
        self.x = self.pickle.x
        print self.x

if __name__ == "__main__":
    SomeClass()

#3. SomeClassTest.py

import unittest
from SomeClass import SomeClass
from ClassToPickle import ClassToPickle # REQUIRED_LINE

class SomeClassTest(unittest.TestCase):
    def testA(self):
        sc = SomeClass()
        self.assertEqual(sc.x, 10)

def main():
    unittest.main()

if __name__ == "__main__":
    main()

I ran #1 program first to make pickle file.
And then, when I run program file #2 alone (i.e. enter “python SomeClass.py”), it works.
And, when I run program #3 alone (i.e. enter “python SomeClassTest.py”), it also works.

However, when I run program #3 as “unit-test” in eclipse+pydev, it returns an error message below.

======================================================================
ERROR: testA (SomeClassTest.SomeClassTest)
———————————————————————-
Traceback (most recent call last):
$ File
“/home/tmp/pickle_problem/SomeClassTest.py”, line 9, in
testA
sc = SomeClass()
$ File “/home/tmp/pickle_problem/SomeClass.py”, line 8, in
init
self.pickle = pickle.load(open(“10.pickle”, ‘r’))
$ File “/usr/lib/python2.7/pickle.py”, line 1378, in load
return Unpickler(file).load()
$ File “/usr/lib/python2.7/pickle.py”, line 858, in load
dispatchkey
File “/usr/lib/python2.7/pickle.py”, line 1090, in load_global
klass = self.find_class(module, name)
$ File “/usr/lib/python2.7/pickle.py”, line 1126, in find_class
klass = getattr(mod, name)
$ AttributeError: ‘module’ object has no attribute ‘ClassToPickle’

———————————————————————-
Ran 1 test in 0.002s

FAILED (errors=1)

And also, when I commented out a line that import ClassToPickle class (line 3 in program #3 and commented as “REQUIRED_LINE”), It doesn’t work and return an error message described below.

E
======================================================================
ERROR: testA (main.SomeClassTest)
———————————————————————-
Traceback (most recent call last):
File “SomeClassTest.py”, line 9, in testA
sc = SomeClass()
File “/home/tmp/pickle_problem/SomeClass.py”, line 8, in init
self.pickle = pickle.load(open(“10.pickle”, ‘r’))
File “/usr/lib/python2.7/pickle.py”, line 1378, in load
return Unpickler(file).load()
File “/usr/lib/python2.7/pickle.py”, line 858, in load
dispatchkey
File “/usr/lib/python2.7/pickle.py”, line 1090, in load_global
klass = self.find_class(module, name)
File “/usr/lib/python2.7/pickle.py”, line 1126, in find_class
klass = getattr(mod, name)
AttributeError: ‘module’ object has no attribute ‘ClassToPickle’

———————————————————————-
Ran 1 test in 0.001s

FAILED (errors=1)

I guess the problem is about namespace in python, but I don’t know what happened exactly and what can I do for resolving it.

How can I “run as unit-test (in eclipse+pydev)” #3 program correctly,
and run #3 program in command line without the line which imports ClassToPickle?
Please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:34:40+00:00Added an answer on May 26, 2026 at 12:34 pm

    That because __main__.ClassToPickle != ClassToPickle.ClassToPickle, think of it like this:

    When you pickled the class instance of ClassToPickle in the ClassToPickle.py script, the pickle module will pickle all the reference to the class which mean it will pickle the module name where the class was defined, and because you executed the script ClassToPickle.py this mean that the module will be set to __main__ which mean that pickle module will pickle __main__.ClassToPickle.

    And when you tried to load the pickled instance it fail because it didn’t find the instance’s class which is __main__.ClassToPickle and not the one that you imported using from ClassToPickle import ClassToPickle because this latest is ClassToPickle.ClassToPickle.

    A fix will be to create another script that will handle dumping instead of doing it in ClassToPickle.py e.g.

    import pickle
    
    from ClassToPickle import ClassToPickle
    
    if __name__=="__main__":
        p = ClassToPickle(10)
        pickle.dump(p, open('10.pickle', 'w'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here's the word-count I wrote. I got this error: cannot use obsolete binding at
I got a strange compilation error when I followed the MSDN document to use
I got the error Use of unassigned local variable 'dictionary' despite I assigned the
Why i got this error when i use following code? $customer_key = 'my_key'; $customer_securet
i got a compile error which i do not understand. i have a h/cpp
I'm trying to use AppWeb, and i wrote a very simple program to embed
HI while trying to use soapUI, I got error; SOAP RESPONSE FAILED: this is
Griffon JMX Plugin 0.3 don't work. anyone use this version 1.got error: no lib
I have to use java.util.Calendar in GWT entry point, but I got error while
Error i got is It is an error to use a section registered as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.