ipython magic介绍

mac2025-04-26  7

文章目录

magic命令介绍InteractiveShell ipython的magic相当实用, 而且在集成编辑器里(jupyter, vscode, spyder)中依然能够使用 之前只用了些皮毛, 这次系统总结一下 参考资料: 官方文档 dataquest的一篇文章 掘金推介

magic命令介绍

%xxx line magic %%xxx shell magic 为在ipython命令行执行的命令, 在spyder代码块中无效,如%%time 将按照重要程度/实用程度分级 S

%debug 交互式debug, error时可以调用, 查看变量情况, 从此告别print(), break %pdb 出现error时自动进入debug模式 %store %store var, %store -r var这将使你能够在不同console之间传递变量, 在经常开多个console的spyder异常好用 %precision n 控制原生变量/numpy变量的打印精度 %pylab 可选--no-import-all, 将导入numpy, matplotlib, pylab, mlab, pyplot, display, figsize, getfigs %sx, %system shell execute(或 !!, !)

A

%timeit 精确时间计算 %lsmagic 列举可用magic %quickref %pprint pprint on/off %load file.py, %loadpy, 插入外部文件中的代码 %run xx.ipynb %history -n xxx, xxx例: 12, 4-6, 24/5, ~2/4-~1/2 %env var=xxx, var=$var, %set_env %who, %who_ls 列出所有全局变量,可选参数为类名 %writefile xx.py 将以下代码写入目标文件, 可选参数-a, %cat 查看 %%time 计时(详细) %psearch [options] PATTERN [OBJECT TYPE]

B

%automagic, %autocall 增加便利性的小开关 %cd %pwd %pushd %colors 改变配色方案 %pip %save

InteractiveShell

看别人的ipython notebook时, 经常不明所以地出现InteractiveShell, 这里详细总结一下 参考: 官方文档

from IPython.core.interactiveshell import InteractiveShell # 在执行代码段时, 打印所有输出而非只有最后一个, 约等于matlab InteractiveShell.ast_node_interactivity = "all"
最新回复(0)