首页 > 解决方案 > conda 虚拟环境中的 Jupyter 魔术命令 %%time 错误

问题描述

我使用 jupyter notebook 和 python 3.6 安装。我创建了一个 anaconda 虚拟环境,当我在其中启动 jupyter notebook 时,%%time 命令似乎有问题。

如果我使用 %%time 命令编写一个单元格,如下所示:

%%time
a = 2

我的所有变量声明在我的以下单元格中都是未知的

print(a)

我收到以下错误:

NameError                Traceback (most recent call last)
<ipython-input-3-3f786850e387> in <module>
----> 1 a

NameError: name 'a' is not defined

但是,它在我的根环境中运行良好。请帮忙。

标签: pythonanacondajupyter

解决方案


iPython 7.3 中的行为已更改为以这种方式工作:

https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-time

所以我假设你的根环境必须有旧版本的 iPython / Jupyter notebook。

备选方案:

import time
start = time.time()

"the code you want to test stays here"

end = time.time()
print(end - start)

推荐阅读