この問題はPython issue 3675です。このバグは実際には Python 3.11 で修正されています。
インポートする場合:
from lib2to3.fixes.fix_imports import MAPPING
MAPPING は、Python 2 の名前を Python 3 の名前にマップします。これを逆にしたい。
REVERSE_MAPPING={}
for key,val in MAPPING.items():
REVERSE_MAPPING[val]=key
Unpickler と負荷をオーバーライドできます
class Python_3_Unpickler(pickle.Unpickler):
"""Class for pickling objects from Python 3"""
def find_class(self,module,name):
if module in REVERSE_MAPPING:
module=REVERSE_MAPPING[module]
__import__(module)
mod = sys.modules[module]
klass = getattr(mod, name)
return klass
def loads(str):
file = pickle.StringIO(str)
return Python_3_Unpickler(file).load()
これを pickle.loads の代わりに load と呼びます。
これで問題は解決するはずです。