【发布时间】: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