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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19942653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:02:23  来源:igfitidea点击:

Interactive Python: cannot get `%lprun` to work, although line_profiler is imported properly

pythonprofilingipythonspydermagic-function

提问by Mike Williamson

Problem

问题

Most iPython "magic functions" work fine for me right off the bat: %hist, %time, %prun, etc. However, I noticed that %lpruncould not be found with iPython as I'd installed it originally.

大多数IPython的“神奇功能”的工作对我罚款马上蝙蝠: %hist%time%prun等。然而,我注意到,%lprun无法IPython中找到,因为我想最初安装它。

Attempt to Resolve

尝试解决

I then discovered that I should install the line_profilermodule. I have installed this module, but still cannot seem to get the magic function to work correctly. If I attempt to call %lprun, iPython still cannot find the function. If I call it with the full name ( line_profiler.magic_lprun), the function can be found, but I cannot get it to work at all. Below is an example of what I'd done (which is taken step by step from "Python for Data Analysis" book):

然后我发现我应该安装该line_profiler模块。我已经安装了这个模块,但似乎仍然无法让魔法功能正常工作。如果我尝试调用%lprun,iPython 仍然找不到该函数。如果我使用全名 ( line_profiler.magic_lprun)调用它,则可以找到该函数,但我根本无法使其工作。下面是我所做的一个例子(它是从“Python for Data Analysis”一书中逐步完成的):

Success Using %prun

成功使用 %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)

With this I get a nice answer, as expected:

有了这个,我得到了一个很好的答案,正如预期的那样:

[Out:]

[出去:]

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

And I can do the profiling magic function %prun:

我可以执行分析魔术功能%prun

[In:]

[在:]

%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}

Fail Using %lprun

使用失败 %lprun

But when I try %lprun, I cannot get anything:

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

[In:]

[在:]

%lprun -f add_and_sum add_and_sum(x, y)

[Out:]

[出去:]

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

And if I try to call the function with its standard name, it also does not work:

如果我尝试使用其标准名称调用该函数,它也不起作用:

[In:]

[在:]

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

But the library has been imported properly, or at least this is what it says:

但是该库已正确导入,或者至少它是这样说的:

[In:]

[在:]

line_profiler

[Out:]

[出去:]

<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'>

[In:]

[在:]

line_profiler.magic_lprun

[Out:]

[出去:]

<function line_profiler.magic_lprun>

It seems as if there is something extra that I am supposed to configure so that these new magic functions that I add can be identified as such. I could not find anything via a web search.

似乎我应该配置一些额外的东西,以便可以识别我添加的这些新魔术功能。我无法通过网络搜索找到任何东西。

I am running Spyder as an IDE (still using iPython as the console), but I have also tried it directly with iPython and with iPython notebook. I have had no luck in any format.

我将 Spyder 作为 IDE 运行(仍然使用 iPython 作为控制台),但我也直接使用 iPython 和 iPython notebook 进行了尝试。我没有任何形式的运气。

采纳答案by lmiguelvargasf

You have two ways to make work %lprun, one solution is temporal, i.e., it lasts until you finish your ipythonsession, and the other one is permanent.

您有两种工作方式%lprun,一种是暂时的,即它持续到您完成ipython会话,另一种是永久性的。

Temporal:(as Carlos Cordoba's answer)

时间:(如卡洛斯科尔多瓦的回答)

After starting ipythonrun the following:

启动后ipython运行以下命令:

In [1]: %load_ext line_profiler

Permanent:

永恒的:

Add the following lines to ~/.ipython/profile_default/ipython_config.py:

将以下行添加到~/.ipython/profile_default/ipython_config.py

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

If you don't have the file ~/.ipython/profile_default/ipython_config.py, you can create it by (see thisfor more info):

如果您没有该文件~/.ipython/profile_default/ipython_config.py,则可以通过以下方式创建它(有关更多信息,请参阅内容):

ipython profile create

回答by Carlos Cordoba

To make %lprunwork, you need to load the extension into your session, using this command:

为了%lprun工作,您需要使用以下命令将扩展加载到您的会话中:

In [1]: %load_ext line_profiler

Check out this notebookto see some examples that use the magic.

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

Besides, if you are working with Spyder, there is also a third-party line_profilerplugin, which you can find here.

此外,如果您使用的是 Spyder,还有一个第三方line_profiler插件,您可以在这里找到。