交互式Python:虽然正确导入了line_profiler,但无法使`%lprun`正常工作 [英] Interactive Python: cannot get `%lprun` to work, although line_profiler is imported properly
问题描述
大多数iPython魔术函数对我来说都很好:%hist
, %time
,%prun
等。但是,我注意到%lprun $因为我最初安装了iPython,所以无法找到c $ c>。
Most iPython "magic functions" work fine for me right off the bat: %hist
, %time
, %prun
, etc. However, I noticed that %lprun
could not be found with iPython as I'd installed it originally.
然后我发现我应该安装 line_profiler
模块。我已经安装了这个模块,但似乎仍然无法使魔术功能正常工作。如果我试图调用%lprun
,iPython仍然无法找到该功能。如果我用全名( line_profiler.magic_lprun
)调用它,可以找到该函数,但我根本无法使用它。下面是我所做的一个例子(从Python for Data Analysis一书中逐步采用):
I then discovered that I should install the line_profiler
module. 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):
[在:]
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])
我可以进行分析魔术功能%prun
:
And I can do the profiling magic function %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
,我无法得到任何东西:
Fail Using %lprun
But when I try %lprun
, I cannot get anything:
[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:
[在:]
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:
[在:]
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.
我正在运行Spyder作为IDE(仍然使用iPython作为控制台),但我也直接尝试使用iPython和iPython笔记本。我没有任何格式的运气。
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.
推荐答案
你有两种方法可以做工作%lprun
,一个解决方案是暂时的,即它会持续到你完成 ipython
会话,而另一个是永久性的。
You have two ways to make work %lprun
, one solution is temporal, i.e., it lasts until you finish your ipython
session, and the other one is permanent.
时间:(如Carlos Cordoba的回答)
Temporal: (as Carlos Cordoba's answer)
启动后 ipython
运行以下内容:
In [1]: %load_ext line_profiler
永久:
将以下行添加到〜/ .ipython / profile_default / ipython_config.py
:
c.TerminalIPythonApp.extensions = [
'line_profiler',
]
如果你不这样做有文件〜/ .ipython / profile_default / ipython_config.py
,你可以创建它(参见这个以获取更多信息):
If you don't have the file ~/.ipython/profile_default/ipython_config.py
, you can create it by (see this for more info):
ipython profile create
这篇关于交互式Python:虽然正确导入了line_profiler,但无法使`%lprun`正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!