[Python] ImportError: No module named x while loading a pickle file

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    i'm trying to unpickle a file but i got a module not found error.

    script.py

    import pickle
    with open("file.pkl", "rb") as f:
    data=pickle.load(f)


    and i get this error when i run it: ImportError: No module named copy_reg but if i add 'import copy_reg' for example below import pickle, the script can correctly import it (but then script fails when the code reaches the pickle.load() line)

    this is the relevant part of code in the pickle module:

    pickle.py

    def find_class(self, module, name):
    __import__(module) #this line gives error
    mod = sys.modules[module]
    klass = getattr(mod, name)
    return klass


    Doing some tests i was able to see that no module can be imported (not even builtin module)

    These are the first lines of the pickle file

    file.PKL

    ccopy_reg
    _reconstructor
    p0
    ...


    I'm using python 2.7 Ask me if you need more details and I will provide them, thanks

    Continue reading...

Compartilhe esta Página