84

This might be silly but I am unable to install cPickle with python 3.5 docker image

Dockerfile

FROM python:3.5-onbuild

requirements.txt

cpickle

When I try to build the image

$ docker build -t sample .
Sending build context to Docker daemon 3.072 kB
Step 1 : FROM python:3.5-onbuild
# Executing 3 build triggers...
Step 1 : COPY requirements.txt /usr/src/app/
Step 1 : RUN pip install --no-cache-dir -r requirements.txt
 ---> Running in 016c35a032ee
Collecting cpickle (from -r requirements.txt (line 1))
  Could not find a version that satisfies the requirement cpickle (from -r requirements.txt (line 1)) (from versions: )
No matching distribution found for cpickle (from -r requirements.txt (line 1))
You are using pip version 7.1.2, however version 8.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
The command '/bin/sh -c pip install --no-cache-dir -r requirements.txt' returned a non-zero code: 1
kampta
  • 4,748
  • 5
  • 31
  • 51

3 Answers3

168

cPickle comes with the standard library… in python 2.x. You are on python 3.x, so if you want cPickle, you can do this:

>>> import _pickle as cPickle

However, in 3.x, it's easier just to use pickle.

No need to install anything. If something requires cPickle in python 3.x, then that's probably a bug.

Mike McKerns
  • 33,715
  • 8
  • 119
  • 139
  • 1
    ahh! is `_pickle` replacement for `cPickle` in python 3.x? or `pickle` would be as fast? – kampta May 10 '16 at 13:01
  • 2
    Yes, and `pickle` uses `_pickle`, so it's also fast and can handle subclasses. – Mike McKerns May 10 '16 at 14:14
  • 1
    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:49
  • 1
    @pari: I do not believe that specific error can be generated from the three import statements you have detailed in your comment above. Maybe you should investigate a bit further, and potentially ask a new SO question if it's indeed some new issue or some bug that you are experiencing. – Mike McKerns Jan 25 '19 at 15:25
  • 43
    In Python 2, cPickle is the accelerated version of pickle, and a later addition to the standard library. It was perfectly normal to import it with a fallback to pickle. In Python 3 the accelerated version has been integrated and there is simply no reason to use anything other than `import pickle`. – Martijn Pieters Dec 22 '19 at 20:11
3

You can use this for both python 2 and 3

try:
  import cPickle as pickle
except:
  import pickle
nicosp
  • 56
  • 3
0

I am using python 3.10.9 and trying to run script written in python 2 , So after reading above comments,this commands works . import pickle as pkl

Shikha
  • 229
  • 3
  • 5