如何在 jupyter lab 中 运行 magic 命令

How to run magic command in jupyter lab

我正在尝试 运行 jupyter lab 中的魔术命令,我最近安装了一些来自这个 link 的调试器扩展:

https://github.com/jupyterlab/debugger

当我运行这个命令时:

import numpy as np
from numpy.random import randint

 #A function to simulate one million dice throws.

def one_million_dice():
   return randint(low=1, high=7, size=1000000)
 
%%time
throws = one_million_dice()
mean = np.mean(throws)

错误:

 ---------------------------------------------------------------------------
SyntaxError                               Traceback (most recent call last)
File C:\Users\****\Anaconda3\envs\jupyterlab-debugger\lib\ast.py, in parse:
Line 35:    return compile(source, filename, mode, PyCF_ONLY_AST)

SyntaxError: invalid syntax (<string>, line 1)
---------------------------------------------------------------------------

如何解决?

魔法函数应该放在一个单元格中。 尝试:

import numpy as np
from numpy.random import randint

 #A function to simulate one million dice throws.

def one_million_dice():
   return randint(low=1, high=7, size=1000000)

移至下一个单元格

%%time
throws = one_million_dice()
mean = np.mean(throws)
>>Wall time: 40.9 ms