#author("2019-02-12T21:47:20+00:00","","")
[[Python覚え書き]] >
* IPythonの使い方メモ [#u21fa6e0]
#setlinebreak(on);

#contents
-- 関連
--- [[Python]]
--- [[Python覚え書き]]
-- 参考
--- https://ipython.org
--- https://ipython.readthedocs.io

** 概要 [#s511e3cb]
#html(<div style="padding-left: 10px;">)
IPythonは、Pythonの対話型インタプリタを拡張したもので、入力補完や変数操作、デバッグに役立つ様々な機能が使用できる。
IPython をグラフィカルに利用できるしたものが Jupyter Notebook。なので、IPython のマジックコマンドは [[Jupyter Notebook]] でもそのまま使える。
#html(</div>)

** インストール [#c46e73f2]
#html(<div style="padding-left: 10px;">)
#myterm2(){{
pip install ipython
}}
#html(</div>)

** lineマジック と cellマジック [#d69e12d8]
#html(<div style="padding-left: 10px;">)
IPythonではマジックコマンドと呼ばれる便利な拡張機能があり、1行分のコマンド実行にかかる lineマジックと複数行の実行にかかる cellマジック が利用できる。
lineマジックを利用する場合は %time のようにコマンドの頭に % を1つ付け、cellマジックを利用する場合は %%time のようにコマンドの頭に % を2つ付ける。

例)
#myterm2(){{
In [1]: %time var1 = 'test1'
CPU times: user 3 &#181;s, sys: 0 ns, total: 3 &#181;s
Wall time: 6.2 &#181;s

In [2]: %%time
   ...: count = 0
   ...: for i in range(10000):
   ...:     count += 1
   ...: 
CPU times: user 1.18 ms, sys: 3 &#181;s, total: 1.18 ms
Wall time: 1.18 ms
}}


#html(</div>)

** マジックコマンドのヘルプを表示する [#m06195dd]
#html(<div style="padding-left: 10px;">)

*** マジックコマンドの一覧表示 [#c3b3a5da]
#html(<div style="padding-left: 10px;">)
#myterm2(){{
In [1]: %lsmagic
Out[1]: 
Available line magics:
%alias  %alias_magic  %autocall  %autoindent  %automagic  %bookmark  %cat  %cd  %colors  %config  %cp  %cpaste  %debug  %dhist
  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff
  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %matplotlib  %mkdir  %mv  %notebook  %page  %paste
  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %popd  %pprint  %precision  %profile  %prun  %psearch  %psource  %pushd
  %pwd  %pycat  %pylab  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save
  %sc  %set_env  %store  %sx  %system  %tb  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode

Available cell magics:
%%!  %%HTML  %%SVG  %%bash  %%capture  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%markdown  %%perl  %%prun
  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.
}}
#html(</div>)

*** マジックコマンドのヘルプ [#u3964c23]
#html(<div style="padding-left: 10px;">)

''コマンド名?'' を入力する事でコマンドの具体的なヘルプが表示される。

例)
#myterm2(){{
In [3]: time?
Docstring:
Time execution of a Python statement or expression.

The CPU and wall clock times are printed, and the value of the
expression (if any) is returned.  Note that under Win32, system time
is always reported as 0, since it can not be measured.
  .
  .
  .

Examples
--------
::

  In [1]: %time 2**128
  CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
  Wall time: 0.00
  Out[1]: 340282366920938463463374607431768211456L
  .
  .
  .
}}
#html(</div>)

#html(</div>)


** マジックコマンド一覧 [#lcc626fb]
#html(<div style="padding-left: 10px;">)

https://ipython.readthedocs.io/en/stable/interactive/magics.html

*** Line magics [#y4534190]
#html(<div style="padding-left: 10px;">)

https://ipython.readthedocs.io/en/stable/interactive/magics.html#line-magics

| コマンド | 説明 | 使用例 |h
| %alias | | |
| %alias_magic | | |
| %autoawait | | |
| %autocall | | |
| %automagic | | |
| %bookmark | | |
| %cd | ディレクトリを移動する | %cd /tmp |
| %colors | | |
| %config | | |
| %debug | | |
| %dhist | ディレクトリの移動履歴を表示する。 | %dhist |
| %dirs | | |
| %edit | 指定したファイルを編集する。 | %edit test1.py |
| %env | 環境変数を表示する。(printenvみたいなもの)| %env |
| %history | コマンドの実行履歴を表示する。| %history |
| %load | | |
| %load_ext | | |
| %loadpy | | |
| %logoff | | |
| %logon | | |
| %logstart | | |
| %logstate | | |
| %logstop | | |
| %lsmagic | マジックコマンドの一覧を表示する。 | |
| %macro | | |
| %magic | | |
| %matplotlib | | |
| %notebook | | |
| %page | | |
| %pastebin | | |
| %pdb | pdbの自動ON/OFF切り替え | %pdf on&br;%run -d test1.py  ...  ファイル実行でipdb起動&br;%debug func1()  ... 関数実行でipdb起動|
| %pdef | | |
| %pdoc | | |
| %pfile | | |
| %pinfo | | |
| %pinfo2 | | |
| %pip | pipを私用する。 | %pip install yaml |
| %popd | | |
| %pprint | | |
| %precision | | |
| %prun | cProfileを使用した関数のボトルネック調査。&br;※%run -p ファイル名でも同様の事が可能 | %prun myfunc() |
| %psearch | パターンにマッチするオブジェクトを検索する。| %psearch time.* |
| %psource | オブジェクトのソースコードを表示する。| %psource datetime |
| %pushd | | |
| %pwd | 現在のディレクトリを表示する。 | %pwd |
| %pycat | | |
| %pylab | | |
| %quickref | マジックコマンド等のクイックリファレンスを表示する。| %quickref  |
| %recall | | |
| %rehashx | | |
| %reload_ext  | | |
| %rerun | 直前の入力コマンドを再実行する。 | %rerun&br;%rerun -l 2 (直前の2行分を再実行) |
| %reset | 宣言した変数等を削除する。| %reset -f |
| %reset_selective  | | |
| %run | 指定したファイルを実行する | |
| %save | IPythonへの入力内容をファイルに保存する。| %save ファイル名 行No-行No 行No-行No ... |
| %sc | シェルの実行結果をキャプチャする。 | In [1]: %sc tmp_files = ls -l /tmp &#124; awk '{print $NF}'&br;In [1]: tmp_files&br;Out[1]: &br;['tmp1.txt',&br;'tmp2.txt'&br;''] |
| %set_env | 環境変数を設定する。| %set_env TESTVAR1="This is Sample Env" |
| %sx  | | |
| %system  | | |
| %tb | 最後に実行した処理のトレースバックを表示する。| %tb |
| %time | 実行時間を計測する | |
| %timeit | 実行時間を計測する。但し何度か実行/計測した平均値を表示する。 | |
| %unalias  | | |
| %unload_ext | | |
| %who | 宣言済みの変数や関数、import 済みのモジュール等の名前をする。| %who |
| %who_ls | 宣言済みの変数や関数、import 済みのモジュール等の名前をリストで取得する。| %who_ls |
| %whos | 宣言済みの変数や関数、import 済みのモジュール等を一覧表示する。| %whos |
| %xdel  | | |
| %xmode  | | |

#html(</div>)

#html(<div style="padding-left: 10px;">)

*** Cell magics [#jdfbb2e7]

https://ipython.readthedocs.io/en/stable/interactive/magics.html#cell-magics

%lsmagic で確認した所、以下のものが利用できる模様。(殆どはPython以外の言語の実行用コマンド)

#myterm2(){{
Available cell magics
%%!  %%HTML  %%SVG  %%bash  %%capture  %%debug  %%file  %%html  %%javascript  %%js  %%latex
  %%markdown  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script
  %%sh  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile
}}
#html(</div>)

#html(</div>)


トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS