Using pickle in 1.10.x

I am currently working on a program that I am converting from P3D 1.9.4 to 1.10.7. One of the things that have changed is that cPickle is no longer supported. Look like I need to use direct.stdpy.pickle instead.

Currently, my code looks like this:

import cPickle

fo = open(“myFile.dat”, “w”)
cPickle.dump(someData,fo)
fo.close()

fo = open(“myFile.dat”, “r”)
someOtherData = cPickle.load(fo)
fo.close()

If I am reading correctly this should be the correct syntax now:

from direct.stdpy.pickle import Pickler, Unpickler, dump, dumps, load, loads

fo = open(“myFile.dat”, “w”)
pickle.dump(someData,fo)
fo.close()

fo = open(“myFile.dat”, “r”)
someOtherData = pickle.load(fo)
fo.close()

Is that correct or am I missing something?

Hi there, long time no see!

This is not a change in Panda3D but a change in Python. The default version of Python that ships with Panda3D has changed. It used to be Python 2.7 but is now either 3.6 or 3.7 (I can’t remember exactly).

You can still use Panda3D 1.10 with Python 2.7 and continue using cPickle, but we do recommend fixing your code to be compatible with Python 3.

There are many resources online about the changes between Python 2 and Python 3. One of them is indeed that cPickle was removed. For most intents and purposes, the pickle module should be a drop-in replacement, so you can just replace:

import cPickle

cPickle.load(...)

with:

import pickle

pickle.load(...)

Good luck with the porting effort!

Thanks. I have been around I just do a lot more reading than posting LOL. I should probably update my showcase post. I have another issue I’ll be putting up in another post. Thanks for all the work you and the others continue to do for the community.

1 Like

As a follow up I found that using pickle requires opening the file for reading or writing bitwise while cPckle handles this implicity (which seems to me the more Pythonic way of doing things). So this:

fo = open(“myFile.dat”, “w”)
cPickle.dump(someData,fo)
fo.close()

becomes this:

fo = open(“myFile.dat”, “wb”)
pickle.dump(someData,fo)
fo.close()

Without the change, the operation throws a type error complaining that the data being written needs to be bytes, not a string.