2019年1月19日 星期六

Win 10 安裝 IPython

今天去市圖拿預約書 "PyTorch 讓你愛不釋手", 很快看完前兩章. 第二章介紹了 IPython, 這是增強版的 Python Shell, 加上了自動補全 (按 Tab 鍵) 與內省 (說明) 功能, 比 IDLE 要強大多了, 所以立馬安裝是用看看  :

C:\Users\Tony>pip3 install ipython 
Collecting ipython

  Downloading https://files.pythonhosted.org/packages/f0/b4/a9ea018c73a84ee6280b2e94a1a6af8d63e45903eac2da0640fa63bca4db/ipython-7.2.0-py3-none-any.whl (765kB)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python36\lib\site-packages (from ipython) (0.3.9)
Requirement already satisfied: traitlets>=4.2 in c:\python36\lib\site-packages (from ipython) (4.3.2)
Collecting pygments (from ipython)
  Downloading https://files.pythonhosted.org/packages/13/e5/6d710c9cf96c31ac82657bcfb441df328b22df8564d58d0c4cd62612674c/Pygments-2.3.1-py2.py3-none-any.whl (849kB)
Collecting backcall (from ipython)
  Downloading https://files.pythonhosted.org/packages/84/71/c8ca4f5bb1e08401b916c68003acf0a0655df935d74d93bf3f3364b310e0/backcall-0.1.0.tar.gz
Collecting jedi>=0.10 (from ipython)
  Downloading https://files.pythonhosted.org/packages/c2/bc/54d53f5bc4658380d0eca9055d72be4df45e5bfd91a4bac97da224a92553/jedi-0.13.2-py2.py3-none-any.whl (177kB)
Collecting pickleshare (from ipython)
  Downloading https://files.pythonhosted.org/packages/9a/41/220f49aaea88bc6fa6cba8d05ecf24676326156c23b991e80b3f2fc24c77/pickleshare-0.7.5-py2.py3-none-any.whl
Collecting prompt-toolkit<2 .1.0="" from="" gt="" ipython="" p="">  Downloading https://files.pythonhosted.org/packages/d1/e6/adb3be5576f5d27c6faa33f1e9fea8fe5dbd9351db12148de948507e352c/prompt_toolkit-2.0.7-py3-none-any.whl (338kB)
Requirement already satisfied: setuptools>=18.5 in c:\python36\lib\site-packages (from ipython) (38.5.1)
Requirement already satisfied: decorator in c:\python36\lib\site-packages (from ipython) (4.3.0)
Requirement already satisfied: ipython-genutils in c:\python36\lib\site-packages (from traitlets>=4.2->ipython) (0.2.0)
Requirement already satisfied: six in c:\python36\lib\site-packages (from traitlets>=4.2->ipython) (1.11.0)
Collecting parso>=0.3.0 (from jedi>=0.10->ipython)
  Downloading https://files.pythonhosted.org/packages/09/51/9c48a46334be50c13d25a3afe55fa05c445699304c5ad32619de953a2305/parso-0.3.1-py2.py3-none-any.whl (88kB)
Collecting wcwidth (from prompt-toolkit<2 .1.0="" gt="" ipython="" p="">  Downloading https://files.pythonhosted.org/packages/7e/9f/526a6947247599b084ee5232e4f9190a38f398d7300d866af3ab571a5bfe/wcwidth-0.1.7-py2.py3-none-any.whl
Building wheels for collected packages: backcall
  Running setup.py bdist_wheel for backcall ... done
  Stored in directory: C:\Users\Tony Huang\AppData\Local\pip\Cache\wheels\98\b0\dd\29e28ff615af3dda4c67cab719dd51357597eabff926976b45
Successfully built backcall
Installing collected packages: pygments, backcall, parso, jedi, pickleshare, wcwidth, prompt-toolkit, ipython
Successfully installed backcall-0.1.0 ipython-7.2.0 jedi-0.13.2 parso-0.3.1 pickleshare-0.7.5 prompt-toolkit-2.0.7 pygments-2.3.1 wcwidth-0.1.7


相依模組還真是不少哩! 

我以前已安裝過 PyTorch, 那就用它來試試自動補全功能吧! 在命令列輸入 IPython 進入 REPL 介面, 先用 import torch as t 引入 PyTorch 模組, 然後輸入 t.Float 後按 Tab 鍵, 果真出現自動補全提示選單 :




按上下鍵可以移動選項, 按 Enter 即填入選定的項目, 好用. 

在 [In] 輸入列按上下鍵可以回溯顯示之前輸入過的指令, 可叫出來修改或重新執行, 省卻重新 Key 指令的麻煩. 

另外, 它還提供以 % 開頭的魔法指令如下 :

 IPython 魔法指令 說明
 %timeit command 執行 command 指令並計算其執行時間
 %hist 顯示輸入歷史
 %paste 貼上剪貼簿中的指令
 %cat test.py 顯示 test.py 內容
 %run -i  test.py 在目前命名空間中執行 test.py
 %quickref 顯示快速參考
 %who 顯示目前命名空間中的全部變數
 %debug 進入偵錯模式 (輸入 q 退出)
 %env 顯示系統環境變數
 %xdel variable 刪除變數 variable 及其全部參考
 %magic 顯示全部魔法指令

魔法指令也支援說明功能, 在指令後面加 "?" 會顯示指令之說明文件,  加 "??" 會顯示指令之原始碼, 例如 :

C:\Users\Tony>ipython 
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import torch as t      #引入 PyTorch

In [2]: a=t.Tensor(3,4)         #建立一個 3*4 矩陣

In [3]: %timeit a.sum()       #計算矩陣元素和與所耗時間
4.65 µs ± 77.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [4]: %hist                         #顯示下過的指令
import torch as t
a=t.Tensor(3,4)
%timeit a.sum()
%hist


In [5]:


沒有留言 :