[PYTHON] Pickle.dumped object dies when pickle.loaded in another object hierarchy

|--b
|  |--unpickling.py
|--lib
|  |--__init__.py
|  |--__init__.pyc
|  |--class_a.py
|  |--class_a.pyc
|--pickling.py
|--unpickling.py
|--class_a.pkl

lib/class_a.py


class ClassA:
    pass

pickling.py


import pickle
from lib.class_a import ClassA

a = ClassA()

with open('class_a.pkl', 'w') as f:
    pickle.dump(a,f)

unpickling.py


# b/pickling.Same for py

import sys
import pickle

filepath = sys.argv[1]

with open(filepath, 'r') as f:
    a = pickle.load(f)

And if you do this, you will die

[~]> python pickling.py
[~]> cd b
[~/b]> python unpickling.py ../class_a.pkl
Traceback (most recent call last):
  File "unpickling.py", line 7, in <module>
    a = pickle.load(f)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1384, in load
    return Unpickler(file).load()
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 864, in load
    dispatch[key](self)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1075, in load_inst
    klass = self.find_class(module, name)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1130, in find_class
    __import__(module)
ImportError: No module named lib.class_a

I will live if I do this

[~]> python pickling.py
[~]> python unpickling.py ../class_a.pkl

Recommended Posts

Pickle.dumped object dies when pickle.loaded in another object hierarchy
Behavior when saving python datetime object in MongoDB
When the selected object in bpy.context.selected_objects is not returned