51

I am trying to use cPickle on a windows box, using Anaconda. I am using python 3.5. I am not using a virtualenv (though probably should be).

When I try to import cPickle I get "ImportError: No module named 'cPickle'"

Python 3.5.0 |Anaconda custom (64-bit)| (default, Dec  1 2015, 11:46:22) [MSC v.
1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cPickle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'cPickle'

My understanding is that cPickle comes built in with Python 3.5, so I can't understand why cPickle is not found. Any idea what has gone wrong/how I can clean things up/how to troubleshoot the issue.

Tom Walker
  • 837
  • 1
  • 8
  • 12

2 Answers2

94

There is no cPickle in Python 3. Just import pickle. pickle will automatically use the C accelerator.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • 1
    Neat, so pickle in 3 is as efficient as previous versions cPickle, and pickle as-was was deprecated? Thanks! This was not easy information to find. – Tom Walker Mar 30 '18 at 18:17
  • I am using Python 3.6.6, and tried everything: import pickle as cPickle, import pickle, import _pickle as cPickle but still having the error "ModuleNotFoundError: No module named 'cPickle' " – khushbu Jan 25 '19 at 09:46
  • @pari: You've probably got a stale pyc file or a poorly-chosen filename for one of your files (like pickle.py, perhaps). In any case, the comments on this answer are not the place to ask debugging questions. Hit the "Ask Question" button and write up your question, including code and complete, exact error message. – user2357112 Jan 25 '19 at 09:57
  • is there a difference because the original code uses cPickel and i use pickle and get this Error message: UnicodeDecodeError: 'ascii' codec can't decode byte 0xbe in position 3: or – campy Oct 22 '20 at 09:54
  • @campy: You have an unrelated bug, likely a text/bytes problem. – user2357112 Oct 22 '20 at 09:56
  • in my case 'latin1' encoding helped. pkl = pickle.load(open('mydata.dat', 'rb'), encoding='latin1') – campy Oct 22 '20 at 10:29
11

try import pickle as cPickle. this way you don't have to edit much

Eshaka
  • 974
  • 1
  • 14
  • 38