交互式Python:尽管已正确导入line_profiler,但无法使`%lprun`正常工作

46

问题

对我来说,大多数iPython“magic functions”直接运行就可以了:%hist%time%prun等。但是,我注意到原始安装的iPython中找不到%lprun

解决尝试

然后,我发现我应该安装line_profiler模块。我已经安装了这个模块,但似乎不能正确使用魔术函数。如果我尝试调用%lprun,iPython仍然找不到这个函数。如果我使用全名(line_profiler.magic_lprun)来调用,可以找到该函数,但我无法使其正常工作。以下是我所做的示例(从《Python数据分析》书籍逐步取得):

使用%prun成功

[In:]

def add_and_sum(x, y):
    added = x + y
    summed = added.sum(axis=1)
    return summed

x = randn(3000, 3000)
y = randn(3000, 3000)

add_and_sum(x, y)

我得到了一个很好的答案,如预期:

[输出:]

array([-23.6223074 , -10.08590736, -31.2957222 , ..., -14.17271747,
    63.84057725, -50.28469621])

我可以执行分析神奇函数%prun:

[输入:]

%prun add_and_sum(x, y)

[Out:]

6 function calls in 0.042 seconds

Ordered by: internal time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.020    0.020    0.029    0.029 <ipython-input-27-19f64f63ba0a>:1(add_and_sum)
    1    0.013    0.013    0.042    0.042 <string>:1(<module>)
    1    0.009    0.009    0.009    0.009 {method 'reduce' of 'numpy.ufunc' objects}
    1    0.000    0.000    0.009    0.009 _methods.py:16(_sum)
    1    0.000    0.000    0.009    0.009 {method 'sum' of 'numpy.ndarray' objects}
    1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

使用%lprun失败

但是当我尝试使用%lprun时,我什么也得不到:

[In:]

%lprun -f add_and_sum add_and_sum(x, y)

[Out:]

ERROR: Line magic function `%lprun` not found.

如果我尝试使用标准名称调用该函数,它也无法工作:

[输入:]

line_profiler.magic_lprun -f add_and_sum.test test.add_and_sum(x, y)

[Out:]

line_profiler.magic_lprun -f add_and_sum.test test.add_and_sum(x, y)
                                       ^
SyntaxError: invalid syntax

但是图书馆已经被正确导入,或者至少它是这样说的:

[在:]

line_profiler

[输出:]
<module 'line_profiler' from '/Users/<edit>/anaconda/lib/python2.7/site-packages/line_profiler-1.0b3-py2.7-macosx-10.5-x86_64.egg/line_profiler.pyc'>

[在:]
line_profiler.magic_lprun

[Out:]

<function line_profiler.magic_lprun>

似乎我需要配置一些额外的东西,以便这些新的魔术函数可以被识别为此类函数。我在网上搜索后没有发现任何有用信息。

我正在使用Spyder作为IDE(仍然使用iPython作为控制台),但我也尝试过直接在iPython和iPython笔记本上运行。无论使用哪种方式都没有成功。

2个回答

70

要使%lprun工作,您需要使用此命令将扩展加载到会话中:

In [1]: %load_ext line_profiler

查看此笔记本以查看使用该魔法的一些示例。

此外,如果您正在使用Spyder,则还有一个第三方line_profiler插件,您可以在此处找到。


太棒了,非常感谢你,Carlos!!这对我有用...我似乎在网上找不到这些信息。 - Mike Williamson
2
如果你正在使用Spyder,那么line_profiler插件是一个不错的选择。我发现这是运行line_profiler最少麻烦的方式,并且它提供的输出结果也很好。 - cd98
1
很抱歉地说,它不再有效(自IPython 5以来)。 - Alleo
@Alleo,不是我的错 :-) line_profiler 扩展还没有更新以适用于 IPython 5.0。请查看 https://github.com/rkern/line_profiler/pull/65 进行修复。 - Carlos Cordoba
1
%load_ext line_profiler 命令适用于 IPython 5.0 版本和 line_profiler 2.0 版本。 - Steven C. Howell

30

你有两种方法来让%lprun工作,一种解决方案是临时的,即它只持续到你完成ipython会话结束,另一种则是永久的。

临时的: (如Carlos Cordoba的回答)

在启动ipython后运行以下命令:

In [1]: %load_ext line_profiler

永久性:

将以下行添加到~/.ipython/profile_default/ipython_config.py文件中:

c.TerminalIPythonApp.extensions = [
    'line_profiler',
]

如果您没有文件~/.ipython/profile_default/ipython_config.py,您可以通过以下方式创建(有关更多信息,请参见此处):

ipython profile create

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接