首页 > 解决方案 > Jupyter Notebook:%%time 魔法丢失全局变量

问题描述

在 jupyter 单元格内声明的变量%% time不会显示在后续单元格中。

In [1]: %%time
        a = 2

In [2]: print(a)

NameError     Traceback (most recent call last)
<ipython-input-5-bca0e2660b9f> in <module>
----> 1 print(a)

NameError: name 'a' is not defined

这是一个错误还是设计使然?我不记得以前遇到过这个问题。

我在 Windows 10 上使用 Python 3.7.3 和 Jupyter 4.4.0 和 IPython 7.4.0。

标签: pythonjupyter-notebookipython

解决方案


对于以后遇到这个问题的人来说,这是 Ipython 7.4 中的一个错误。错误报告在这里 - https://github.com/ipython/ipython/issues/11659

它在 Ipython 7.5 中得到修复。

使用将 ipython 更新到 7.5conda update ipython解决了该问题。


推荐阅读