I have a project with a function foo
in a module my_project.my_functions
. I want to pickle that function in a way that I can unpickle it from somewhere else without requiring to import my_project
. foo
does not have any side effect, so no dependencies outside the function.
I'm using dill
to pickle foo
, but dill is saving it as a <function my_project.my_functions.foo>
, and complains about the unknown my_project
module when I try to unpickle it.
Any solution?
I solved it by recreating the function from the code giving and empty globals
dictionary.
def f(n):
return n+1
import dill
import types
import module
f = types.FunctionType(module.f.__code__,{})
with open("my_func.pkl", 'wb') as fs:
dill.dump(f, fs)
import dill
with open("my_func.pkl", 'rb') as fs:
f = dill.load(fs)