Skip to content

Errors when loading in python3 a file dumped in python2 #394

@tayciryahmed

Description

@tayciryahmed

Hi,

I am trying to load in python3.7 a file - sklearn pipeline that was serialised using dill.dump in python2.7.
I am following this tutorial: https://rebeccabilbro.github.io/convert-py2-pickles-to-py3/

I get the below error :

import dill, pickle 
dill._dill._reverse_typemap["ObjectType"] = object
with open(sklearn_pipeline, "rb") as f:
    loaded = pickle.load(f, encoding="latin1")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'dill.dill'

dill.__version__
'0.3.3'

Note that loading a simple sklearn classifier, works well with this snippet.

=> I tried to downgrade dill to v0.2.5, I get the following error:

pip install dill==0.2.5
import dill, pickle 
dill.dill._reverse_typemap["ObjectType"] = object
with open(sklearn_pipeline, "rb") as f:
       loaded = pickle.load(f, encoding="latin1")

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/local/lib/python3.7/site-packages/dill/dill.py", line 503, in _load_type
    return _reverse_typemap[name]
KeyError: 'TypeType'

Then, I tried to set dill.dill._reverse_typemap["TypeType"] = type I get the below error:

pip install dill==0.2.5
import dill, pickle 
dill.dill._reverse_typemap["ObjectType"] = object
dill.dill._reverse_typemap["TypeType"] = type
with open(sklearn_pipeline, "rb") as f:
       loaded = pickle.load(f, encoding="latin1")

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: an integer is required (got type str)

I also tried the bytes encoding and it returns the same error: TypeError: an integer is required (got type str).

Using dill==0.3.3:

import dill
dill._dill._reverse_typemap["ClassType"] = type
dill._dill._reverse_typemap["TypeType"] = type
dill._dill._reverse_typemap["ObjectType"] = object
with open(sklearn_pipeline, "rb") as model_file:
    u = dill.Unpickler(model_file)
    u.encoding = "latin1"
    loaded = u.load()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/dill/_dill.py", line 481, in load
    obj = StockUnpickler.load(self)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x83 in position 32: ordinal not in range(128)
import dill
dill._dill._reverse_typemap["TypeType"] = type
dill._dill._reverse_typemap["ObjectType"] = object
with open(sklearn_pipeline, "rb") as f:
    loaded = dill.load(f, encoding="latin1")

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/local/lib/python3.7/site-packages/dill/_dill.py", line 278, in load
    return Unpickler(file, ignore=ignore, **kwds).load()
  File "/usr/local/lib/python3.7/site-packages/dill/_dill.py", line 481, in load
    obj = StockUnpickler.load(self)
TypeError: an integer is required (got type str)

Any ideas on how to solve this ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions