2

I am trying to cache some variables in Jupyter notebook cells that are big and take long to compute. I use Ubuntu 20.04 and Python 3.8. I added ipycache in my pyproject.toml file and it was successfully installed with Poetry. Then, I added %load_ext ipycache in a cell and when I execute it I get:

ModuleNotFoundError                       Traceback (most recent call last)
/tmp/ipykernel_47459/791109313.py in <module>
----> 1 get_ipython().run_line_magic('load_ext', 'ipycache')

~/.cache/pypoetry/virtualenvs/toxic-comments-bias-kaggle-yMytpa1p-py3.8/lib/python3.8/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2362                 kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2363             with self.builtin_trap:
-> 2364                 result = fn(*args, **kwargs)
   2365             return result
   2366 

~/.cache/pypoetry/virtualenvs/toxic-comments-bias-kaggle-yMytpa1p-py3.8/lib/python3.8/site-packages/decorator.py in fun(*args, **kw)
    230             if not kwsyntax:
    231                 args, kw = fix(args, kw, sig)
--> 232             return caller(func, *(extras + args), **kw)
    233     fun.__name__ = func.__name__
    234     fun.__doc__ = func.__doc__

~/.cache/pypoetry/virtualenvs/toxic-comments-bias-kaggle-yMytpa1p-py3.8/lib/python3.8/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

~/.cache/pypoetry/virtualenvs/toxic-comments-bias-kaggle-yMytpa1p-py3.8/lib/python3.8/site-packages/IPython/core/magics/extension.py in load_ext(self, module_str)
     31         if not module_str:
     32             raise UsageError('Missing module name.')
---> 33         res = self.shell.extension_manager.load_extension(module_str)
     34 
     35         if res == 'already loaded':
~/.cache/pypoetry/virtualenvs/toxic-comments-bias-kaggle-yMytpa1p-py3.8/lib/python3.8/site-packages/IPython/core/extensions.py in load_extension(self, module_str)
     78             if module_str not in sys.modules:
     79                 with prepended_to_syspath(self.ipython_extension_dir):
---> 80                     mod = import_module(module_str)
     81                     if mod.__file__.startswith(self.ipython_extension_dir):
     82                         print(("Loading extensions from {dir} is deprecated. "

/usr/lib/python3.8/importlib/__init__.py in import_module(name, package)
    125                 break
    126             level += 1
--> 127     return _bootstrap._gcd_import(name[level:], package, level)
    128 
    129 

/usr/lib/python3.8/importlib/_bootstrap.py in _gcd_import(name, package, level)

/usr/lib/python3.8/importlib/_bootstrap.py in _find_and_load(name, import_)

/usr/lib/python3.8/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

/usr/lib/python3.8/importlib/_bootstrap.py in _load_unlocked(spec)

/usr/lib/python3.8/importlib/_bootstrap_external.py in exec_module(self, module)

/usr/lib/python3.8/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

~/.cache/pypoetry/virtualenvs/toxic-comments-bias-kaggle-yMytpa1p-py3.8/lib/python3.8/site-packages/ipycache.py in <module>
      9 
     10 # Stdlib
---> 11 import inspect, os, sys, textwrap, cPickle
     12 
     13 # Our own

ModuleNotFoundError: No module named 'cPickle'

I thought that pickle is used by default in Python >=3, and whenever cPickle is imported, then it automatically uses pickle instead. However, this import (import inspect, os, sys, textwrap, cPickle) is not on my end, so I cannot do something like import pickle as cPickle to fix this error. It also seems ipycache is not maintained anymore. Is it possible to resolve this issue from my side, or should I try a different caching method?

KLaz
  • 446
  • 3
  • 11

2 Answers2

1

As of the latest released Pip version (0.1.14), ipycache has not fixed this known bug (ipycache Github issue). However, there is an apparent plan to release a fix to this in an upcoming release (0.1.5). In the mean time, that ipycache issue gives a solution of using the latest developer version with

pip install git+https://github.com/rossant/ipycache --upgrade

which I can confirm worked for me as it worked for others commenting on the Github issue.

riiily
  • 11
  • 3
0

I also could not get it running in an Azure Databricks environment as of 2022 because of the same cPickle import error. This question has alternatives to ipycache.

The Freeze jupyter notebook extension seems to offer the almost the same caching functionality.

Olavo Sampaio
  • 26
  • 1
  • 5