No module named 'cPickle'

Hi Friends, when I try to run the code for lesson notebook, I get the error as below. Can you pls guide me on how to fix it?

I created new folder called Lesson1 and copied lesson1.ipynb, and into the new folder and tried to execute the notebook file line by line.

ModuleNotFoundError Traceback (most recent call last)
in ()
----> 1 import utils; reload(utils)
2 from utils import plots

/Users/kprayagai/Documents/Magesh/MachineLearning/JeremyTutorials/lesson1/ in ()
1 from future import division,print_function
2 import math, os, json, sys, re
----> 3 import cPickle as pickle
4 from glob import glob
5 import numpy as np

ModuleNotFoundError: No module named ‘cPickle’

Here are my theano and tensor versions.

tensorflow: 1.0.0
Using Theano backend.
keras: 2.0.4

Python 3.6.0

Pls let me know if you need more details.


Since you’re using python 3 try updating to

import pickle


Hi Brendan - Thanks. I already see an import ‘import cPickle as pickle’ in Do you mean to replace
"import cPickle as pickle" with “import pickle”?

Hi - I did replace “import cPickle as pickle” with “import pickle” but now am getting an error that No module named ‘bcolz’.

Haha you’re living up to your username :wink: Try pip installing bcolz and Google for stack overflow posts if you continue to see errors.

1 Like

Hi - I fixed that issue by installing bcolz. But now am getting a different error now.

from keras.regularizers import l2, activity_l2, l1, activity_l1
43 from keras.layers.normalization import BatchNormalization
44 from keras.optimizers import SGD, RMSprop, Adam

ImportError: cannot import name ‘activity_l2’

I also see a Warning as below. I think it is something to do with installation or configuration but not able to figure out the problem. I tried to see what is there in keras.json, but I could not find a folder called ~/.keras itself. Not sure how to figure out the problem. But I did install keras.

WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string.
Using TensorFlow backend.

I tried executing further, but face another issue on vgg16 as well. May be is this because of the above issue?

import vgg16; reload(vgg16)
3 from vgg16 import Vgg16

NameError: name ‘reload’ is not defined

If you are the newbie then recommended way is to use Python 2.7.x and Keras 1.x .

The lessons in part 1 were made with that and errors you are getting because of version mismatch.

Hi - I tried to downgrade keras, but I get an error when I try to. (tried the solution as per one of the posts from google)

conda install keras=1.2.2

PackageNotFoundError: Package missing in current osx-64 channels:

  • keras 1.2.2*

I followed the post - Keras 2 Released, and fixed most of the errors due to keras now. But am still getting error - NameError: name ‘reload’ is not defined.

Hope there are other similar users who has faced the same, and hoping to find some solution there.

Thanks for all the friends, for the help and guidance.

This error is coming as reload method has been changed in new Python.

So as I already suggested.

Please use virtual environment or conda to create new python env which is based on 2.7.x

If you use anaconda then command is

conda create -n python2 python=2.7 anaconda

The use it by

source activate python2

Install all packages in this environment

sudo pip install <>

for example

sudo pip install keras

This will work for everything without error.

1 Like

Hi - Thanks. I will try to set up python 2.7 based environment and try the same. Thanks for the help.

If you see NameError: name 'reload' is not defined I’d recommend just removing the reload(vgg16) part, I’m not sure it is necessary anyway.

Thanks Matt.

The reload problem in Python3.X can be solved as following:

from imp import reload

In Python 2.x, this was a builtin, but in 3.x, it’s in the imp module.


Why the change from cPickle to pickle? - see here, but in summary…

In Python 2, you can speed up your pickle access with cPickle. (In Python3, importing pickle will automatically use the accelerated version if it is available.)

An alternative to pip installing bcolz is to install it using Anaconda Navigator. Go to the Environments tab and search for the bcolz package.

Did you download the data? If you don’t have a data directory off your nbs directory, you’ll get all sorts of errors.