Does the dill python module handle importing modules when sys.path differs? -
i'm evaluating dill , want know if scenario handled. have case import module in python process. can use dill serialize , load module in different process has different sys.path doesn't include module? right import failures maybe i'm doing wrong.
here's example. run script foo.py module's path in sys.path:
% cat dill_dump.py import dill import foo myfile = "./foo.pkl" fh = open(myfile, 'wb') dill.dump(foo, fh)
now, run script not have foo.py's directory in pythonpath:
% cat dill_load.py import dill myfile = "./foo.pkl" fh = open(myfile, 'rb') foo = dill.load(fh) print foo
it fails stack trace:
traceback (most recent call last): file "dill_load.py", line 4, in <module> foo = dill.load(fh) file "/home/b/lib/python/dill-0.2.4-py2.6.egg/dill/dill.py", line 199, in load obj = pik.load() file "/rel/lang/python/2.6.4-8/lib/python2.6/pickle.py", line 858, in load dispatch[key](self) file "/rel/lang/python/2.6.4-8/lib/python2.6/pickle.py", line 1133, in load_reduce value = func(*args) file "/home/b/lib/python/dill-0.2.4-py2.6.egg/dill/dill.py", line 678, in _import_module return __import__(import_name) importerror: no module named foo
so, if need have same python path between 2 processes, what's point of serializing python module? or in other words, there advantage loading foo via dill on having "import foo" call?
that's interesting failure. notice if dill.dumps(foo)
contents of module foo
… part fails using python's built-in import hook (__import__
) little more register module sys.modules
. should possible work around , modify dill
module imported if module not found in pythonpath. however, think it's proper module have found in pythonpath… expected of module… i'm not sure if it's idea. might be...
as noted above, file foo.py
, contents: hello = "hello world, foo"
>>> import dill >>> import foo >>> dill.dumps(foo) '\x80\x02cdill.dill\n_import_module\nq\x00u\x03fooq\x01\x85q\x02rq\x03}q\x04(u\x08__name__q\x05h\x01u\x08__file__q\x06u\x06foo.pyq\x07u\x05helloq\x08u\x15hello world, fooq\tu\x07__doc__q\nnu\x0b__package__q\x0bnub.'
you can see contents of file preserved in pickle.
the reason use dill
modules, dill
can record dynamic modifications modules. example, adding function or other object:
>>> import foo >>> import dill >>> foo.a = 100 >>> open('foo.pkl', 'w') f: ... dill.dump(foo, f) ... >>>
then restarting… (with foo
in pythonpath)
python 2.7.10 (default, may 25 2015, 13:16:30) [gcc 4.2.1 compatible apple llvm 5.1 (clang-503.0.40)] on darwin type "help", "copyright", "credits" or "license" more information. >>> import dill >>> open('foo.pkl', 'r') f: ... foo = dill.load(f) ... >>> foo.hello 'hello world, foo' >>> foo.a 100 >>>
i've added bug report / feature request: https://github.com/uqfoundation/dill/issues/123
Comments
Post a Comment