Search code examples
pythonserializationpickle

How to I change import statements in a pickle file?


I recently removed a module xyz and replace it with another one of a different name abc. In some pickle files, it still imports the xyz module. I want to change it to import the abc module. How can I do this?

I don't have much experience with serialization/deserialization. My understanding is I'd have to read in the pickle file (i.e., deserialize) and somehow make the changes and then serialize it again after the changes.


Solution

  • Instead of modifying the pickle file, you can "synthetically" import abc as xyz by manually updating sys.modules:

    import sys
    
    import abc
    
    sys.modules["xyz"] = abc
    

    Any attempts to import xyz will use the already loaded xyz module, which is just an alias of abc.

    import xyz
    
    >>> print(xyz)
    <module 'abc' ...>
    

    After inserting the relevant modules into sys.modules, you will be able to load the old pickle file without any modifications to the file itself.

    Since the deserialized objects will have been loaded using the members of abc, if you immediately re-pickle them, you'll get a pickle file that only contains references to abc (and not xyz).