Skip to main content
 首页 » 编程设计

python之IPython 2.x 中的计时和分析

2025年05月04日8youxin

我找到了以下文章:Timing and Profiling in IPython从 2013 年 3 月开始,但我在定义和使用魔法时遇到了问题。我想知道这是不是因为文章中的某些信息不再适用。

我做到了:

$ pip install line-profiler 
$ pip install psutil 
$ pip install memory_profiler  

然后在我的 IPython session 中我定义了:

import memory_profiler 
 
def load_ipython_extension(ip): 
    ip.define_magic('memit', memory_profiler.magic_memit) 
    ip.define_magic('mprun', memory_profiler.magic_mprun) 

当我然后尝试:

%memit

我得到:错误:未定义线魔术函数。为什么?

此外,2013 年的文章是否仍与 IPython 2.x 相关?

请您参考如下方法:

您还需要“注册模块”,如后文所述。

Edit ~/.ipython/profile_default/ipython_config.py, search for, uncomment, and modify these lists to include:

c.TerminalIPythonApp.extensions = [ 'line_profiler_ext', 'memory_profiler_ext', ] c.InteractiveShellApp.extensions = [ 'line_profiler_ext', 'memory_profiler_ext', ]

当我定义配置文件和扩展文件夹 $IPythonDIR/extensions 时,这对我有用。

不确定,如果您在交互式 shell 中手动导入函数,如何使它工作。