【问题标题】:Jupyter Notebook: %%time magic loses global variablesJupyter Notebook:%%time 魔法丢失全局变量
【发布时间】:2019-05-29 23:20:56
【问题描述】:

在 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。

【问题讨论】:

  • 我通常使用%%timeit,它会多次运行表达式。计时不会“渗入”全局命名空间是正常的。
  • @hpaulj jupyter 魔法不是这样工作的——它只运行一次单元格并计算执行时间。这似乎是 IPython 7.4 中的一个错误。在这里找到错误报告:github.com/ipython/ipython/issues/11659

标签: python jupyter-notebook ipython


【解决方案1】:

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

它已在 Ipython 7.5 中得到修复。

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

【讨论】:

    猜你喜欢
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    相关资源
    最近更新 更多