当我们在 jupyter notebook上写下这两行代码:

%load_ext cython
%% cython

往往会出现UsageError: %%cython is a cell magic, but the cell body is empty的错误。怎么办?

使用下面的命令:

%%writefile foo.py
print('Hello world')
%load_ext cython
%%cython
def f(x):
    return 5.0*x
f(10)

这个时候我们的根目录中便会出现foo.pyx文件,然后怎么办呢?

当出现foo.pyx文件时,我们便没必要害怕了,因为此时输入代码:

%%cython
cimport cython

并不会报错了,问题解决