【问题标题】:Jupyter magic command %%time buggy in conda virtual environmentconda 虚拟环境中的 Jupyter 魔术命令 %%time 错误
【发布时间】:2019-10-02 09:11:28
【问题描述】:

我使用 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

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

【问题讨论】:

    标签: python anaconda jupyter


    【解决方案1】:

    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)
    

    【讨论】:

    • 哦,好的,谢谢。你知道在以前的版本中有什么类似于 %%time 的命令吗?
    • 我添加了一个替代答案。不幸的是,它不如魔法好。
    猜你喜欢
    • 2018-04-06
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多