文章目錄
- 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"