Passing python 2.7 object to python 3.9

FWIW: Pickling does have known security risks. As the Python docs say:

Warning
The pickle module is not secure. Only unpickle data you trust.
It is possible to construct malicious pickle data which will execute arbitrary code during unpickling. Never unpickle data that could have come from an untrusted source, or that could have been tampered with.
Consider signing data with hmac if you need to ensure that it has not been tampered with.
Safer serialization formats such as json may be more appropriate if you are processing untrusted data. See Comparison with json.


If you really needed to in a pinch (though probably not a good idea in the long run, as previously explained by the other posters) you can find the relevant data from the ‘old’ object in the .dict attribute of the Py3 input object, and use that to rebuild a new object:

note that you cannot access the .dict attribute though dot-notation, but have to use the getattr() function, due to the preceding . on the key’s name.


If you will be doing a lot of passing objects like this, and know the structure of the classes you are working with ahead of time, it would likely be a better idea to implement a custom serialization / de-serialization though, where you can control and validate any incoming data on the ‘new’ object.

Though again: a better, better idea is probably not to pass objects between interpreters like that in the first place, if you can avoid it.

best
@ed.p.may