I need to pickle a Python3 object to a string which I want to unpickle from an environmental variable in a Travis CI build. The problem is that I can't seem to find a way to pickle to a portable string (unicode) in Python3:
import os, pickle from my_module import MyPickleableClassobj = {'cls': MyPickleableClass, 'other_stuf': '(...)'}pickled = pickle.dumps(obj)# raises TypeError: str expected, not bytes
os.environ['pickled'] = pickled# raises UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb (...)
os.environ['pickled'] = pickled.decode('utf-8')pickle.loads(os.environ['pickled'])
Is there a way to serialize complex objects like datetime.datetime
to unicode or to some other string representation in Python3 which I can transfer to a different machine and deserialize?
Update
I have tested the solutions suggested by @kindall, but the pickle.dumps(obj, 0).decode()
raises a UnicodeDecodeError
. Nevertheless the base64 approach works but it needed an extra decode/encode step. The solution works on both Python2.x and Python3.x.
# encode returns bytes so it needs to be decoded to string
pickled = pickle.loads(codecs.decode(pickled.encode(), 'base64')).decode()type(pickled) # <class 'str'>unpickled = pickle.loads(codecs.decode(pickled.encode(), 'base64'))