Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know if python 3 is using cPickle or Pickle?

I saw these two answers about Pickle and cPickle having dealt automatically by python 3, transparent to the user:

  • https://stackoverflow.com/a/49579373/1273751
  • https://askubuntu.com/a/804618/315699

How can I know if in python 3, it is using the fast version (cPickle) or not? I want to force it to use cPickle. My main interest is not even on the speed, but on the shorter size of the files, since I am generating files that end up being bigger than 1 GB.

like image 225
Homero Esmeraldo Avatar asked Oct 18 '25 08:10

Homero Esmeraldo


1 Answers

Unless you build your Python binary from source, you almost certainly are using the C extension.

However, if you must be certain, check if the _pickle module can be imported:

>>> import _pickle

The pickle module imports _pickle and only if that fails with an ImportError would it be using the pure-Python implementation.

Just to be explicit: what was called cPickle in Python 2 has been renamed to _pickle and is imported transparently, you don't have to do anything explicit to make use of it. That there was a split between pickle and cPickle in Python 2 was just plain confusing.

like image 147
Martijn Pieters Avatar answered Oct 20 '25 23:10

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!