首页 > 解决方案 > 列出加载的 IPython 扩展

问题描述

有没有办法获取当前加载的 IPython 扩展的列表?

例如,如果我使用%load_ext autoreload或通过运行将扩展自动重载加载到 IPython 中:

from IPython import get_ipython
ipython = get_ipython()
ipython.magic("%load_ext autoreload")

有什么方法可以表明我已经加载了这个扩展?

我已经通过直接访问输入历史来尝试这个,比如

from IPython import get_ipython
ipython = get_ipython()
hist = ipython.extract_input_lines("0:100")

但事实证明,IPython 不会在此处或使用In和访问的历史列表中存储带有魔术函数的输入_ih。只有执行普通 Python 的行才会被保存。

无论如何,这个方案一般都行不通。如果一个脚本用runfilerun调用load_ext,历史上会看到的都是runfile('script_name.py', wdir='path/to/wdir').

标签: pythonipython

解决方案


通过查看%load_ext代码来解决这个问题:

from IPython import get_ipython
ip = get_ipython()
ip.extension_manager.loaded

推荐阅读