【问题标题】:ModuleNotFoundError: No module named 'autoreload'ModuleNotFoundError:没有名为“autoreload”的模块
【发布时间】:2018-05-14 15:02:27
【问题描述】:

我使用 python 3 内核在 jupyter notebook 上运行卷积神经网络教程中的以下代码,并得到 ModuleNotFoundError: No module named 'autoreload';

import numpy as np
import h5py          
import matplotlib.pyplot as plt

%matplotlib inline
plt.rcParams['figure.figsize'] = (5.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

%load_ext autoreload   
%autoreload 2          
np.random.seed(1)

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-3d0ea63c7843> in <module>()
      8 plt.rcParams['image.cmap'] = 'gray'
      9 
---> 10 get_ipython().magic('load_ext autoreload   # reload modules before executing user code')
     11 get_ipython().magic('autoreload 2          # Reload all modules (except those excluded by %aimport)')
     12 

/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
   2156         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2157         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2158         return self.run_line_magic(magic_name, magic_arg_s)
   2159 
   2160     #-------------------------------------------------------------------------

/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
   2077                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2078             with self.builtin_trap:
-> 2079                 result = fn(*args,**kwargs)
   2080             return result
   2081 

<decorator-gen-62> in load_ext(self, module_str)

/opt/conda/lib/python3.6/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

/opt/conda/lib/python3.6/site-packages/IPython/core/magics/extension.py in load_ext(self, module_str)
     35         if not module_str:
     36             raise UsageError('Missing module name.')
---> 37         res = self.shell.extension_manager.load_extension(module_str)
     38 
     39         if res == 'already loaded':

/opt/conda/lib/python3.6/site-packages/IPython/core/extensions.py in load_extension(self, module_str)
     81             if module_str not in sys.modules:
     82                 with prepended_to_syspath(self.ipython_extension_dir):
---> 83                     __import__(module_str)
     84             mod = sys.modules[module_str]
     85             if self._call_load_ipython_extension(mod):

ModuleNotFoundError: No module named 'autoreload'   

我只是在这个问题上找不到任何解决方案。我应该如何解决这个错误?

【问题讨论】:

  • 这只是一个导入错误(隐藏在魔法中)。我猜import autoreload 对你来说也失败了。你的 python 路径有问题。在你的路径中应该有一个autoreload.py,通常是.../lib/python3.6/site-packages/IPython/extensions。在sys.path 中寻找类似的东西。
  • 实际上,我按照您的建议找到了 autoreload.py。它位于 ...\Lib\site-packages\IPython\extensions 中。还有什么可能的原因或我接下来应该做什么?
  • 可以直接用import autoreload加载模块吗? extensions 文件夹在 sys.path 中吗?

标签: python-3.x jupyter


【解决方案1】:

模块 autoreload 属于 IPython 库,它在 Jupyter 的后台运行。

同样的错误发生在我身上,这是由于空格引起的,请参阅:

所以确保命令末尾没有空格

【讨论】:

  • 哇!是的!我花了很多时间试图理解为什么import autoreload 有效,为什么%load_ext autoreload 无效。似乎魔术线的读取方式与标准 python 线不同!自动重载后,我确实在行尾有一个空格!
  • 多么烦人的错误!我也被困住了,感谢您为我指明了正确的方向-尾随空格确实是 ipython 魔术的问题
【解决方案2】:

我遇到了同样的错误,因为我在魔术命令之后有一条评论:

不好:

%load_ext autoreload # this comment should be removed

好:

%load_ext autoreload

【讨论】:

  • 我发现即使在%load_ext autoreload之后留一个空格也会导致错误。
  • 我可以确认@Nayef 描述的行为,我有一个尾随空格并得到了同样的错误。
猜你喜欢
  • 2019-03-28
  • 2022-01-07
  • 2017-12-14
  • 2020-12-10
  • 2021-09-02
  • 2020-10-10
  • 2022-01-06
  • 2021-09-10
相关资源
最近更新 更多