1

我正在尝试在 Jupyter 笔记本单元格中缓存一些变量,这些变量很大并且需要很长时间才能计算。我使用 Ubuntu 20.04 和 Python 3.8。我ipycache在我的 pyproject.toml 文件中添加了它,它与 Poetry 一起成功安装。然后,我添加%load_ext ipycache了一个单元格,当我执行它时,我得到:

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'

我认为pickle默认情况下在 Python >=3 中使用它,并且无论何时cPickle导入,它都会自动使用pickle。但是,这个 import ( import inspect, os, sys, textwrap, cPickle) 不是我的目标,所以我不能做类似import pickle as cPickle修复这个错误的事情。它似乎ipycache也不再维护了。是否可以从我这边解决这个问题,或者我应该尝试不同的缓存方法?

4

1 回答 1

0

cPickle由于同样的导入错误,我也无法让它在 2022 年的 Azure Databricks 环境中运行。这个问题有替代方案ipycache

Freeze jupyter notebook 扩展似乎提供了几乎相同的缓存功能。

于 2022-01-24T18:58:14.210 回答