Search code examples
python-3.xpickledill

Python3: pickle a function without side effects


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?


Solution

  • I solved it by recreating the function from the code giving and empty globals dictionary.

    In /my_project/module.py:

    def f(n):
        return n+1 
    

    In my_project, before pickling the function:

    import dill
    import types
    import module
    
    f = types.FunctionType(module.f.__code__,{})
    
    with open("my_func.pkl", 'wb') as fs:
        dill.dump(f, fs)
    

    Somewhere else:

    import dill
    
    with open("my_func.pkl", 'rb') as fs:
        f = dill.load(fs)