2

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

我做了:

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

然后在我的 IPython 会话中我定义了:

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

我得到:ERROR: Line magic function not defined。为什么?

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

4

1 回答 1

1

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

编辑 ~/.ipython/profile_default/ipython_config.py,搜索、取消注释并修改这些列表以包括:

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

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

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

于 2014-04-03T09:08:46.877 回答