python 命名空间

namespace on python pickle

我有一个错误,当我使用unittest泡菜。

3:我写的程序文件

  • 一类是pickled,
  • 这一类的类中使用# 1
  • 在# unittest测试2级。
  • 与真正的代码分别是为您。

    # 1。classtopickle.py

    1
    2
    3
    4
    5
    6
    7
    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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    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()

    我回答# 1第一个程序文件组成的泡菜。然后,当我运行程序文件#独立(2 IU回车"Python someclass.py"),它的工作原理。和,当我运行程序# 3独立的(即"Python someclasstest.py回车"),它的作品。

    然而,当我运行程序# 3"单元测试"+ pydev Eclipse,它返回下面的错误消息。

    ======================================================================
    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)

    当我和因此,该线是进口classtopickle out a(3类线#该计划为"必修3和_线"),它不工作和返回错误消息下面的描述。

    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)

    我想问题是关于命名空间在Python,但我不知道发生了什么事和什么是I do for分辨它。

    Mi-24"运行单元测试(如Eclipse + pydev)"# 3程序正确,# 3程序和运行在命令行没有classtopickle线是进口的吗?请帮助我。


    因为__main__.ClassToPickle != ClassToPickle.ClassToPickle的原因,我们可以这样想:

    当您在ClassToPickle.py脚本中pickle类实例时,pickle模块将pickle所有对该类的引用,这意味着它将pickle定义该类的模块名,因为您执行了脚本ClassToPickle.py,这意味着该模块将被设置为__main__,这意味着pickle。模块将对__main__.ClassToPickle进行酸洗。

    当您试图加载被酸洗的实例时,它失败了,因为它找不到实例的类,该类是__main__.ClassToPickle,而不是您使用from ClassToPickle import ClassToPickle导入的类,因为最新的是ClassToPickle.ClassToPickle

    修复方法是创建另一个脚本来处理转储,而不是在ClassToPickle.py中执行。

    1
    2
    3
    4
    5
    6
    7
    import pickle

    from ClassToPickle import ClassToPickle

    if __name__=="__main__":
        p = ClassToPickle(10)
        pickle.dump(p, open('10.pickle', 'w'))