安裝IPython
pip install ipython
ipython #啟動
或者使用ipython notebook
jupyter notebook#啟動
在IPython里查看幫助文檔
簡單的說逻翁,有三種方法
使用 help 函數(shù)可以查看函數(shù)描述
使用 ? 可以查看函數(shù)洪鸭,對象等的描述和用法
使用 ?? 可以在佳头?的基礎(chǔ)上追加源碼娄柳,前提是該函數(shù)是由python語言編寫的争拐。
使用TAB可以對命令進(jìn)行補(bǔ)全
如果并不知道首字符搀罢,只知道中間的匹配項(xiàng)蝗岖,也可以使用*來做匹配.
In [1]: help(len)
In [2]: len?
In [3]: len??
In [4]: import h<TAB>
hashlib hmac http
heapq html husl
In [5]: *Warning?
BytesWarning RuntimeWarning
DeprecationWarning SyntaxWarning
FutureWarning UnicodeWarning
ImportWarning UserWarning
PendingDeprecationWarning Warning
ResourceWarning
在IPython的一些快捷鍵
Keystroke | Action |
---|---|
Ctrl-a |
光標(biāo)回到行首 |
Ctrl-e |
光標(biāo)回到行末 |
Ctrl-b or the left arrow key |
光標(biāo)后移一位 |
Ctrl-f or the right arrow key |
光標(biāo)前移一位 |
------------------------------- | -------------------------------------------------- |
Backspace key | Delete previous character in line |
Ctrl-d |
刪除光標(biāo)后的字符 |
Ctrl-k |
剪切光標(biāo)后的字符 |
Ctrl-u |
剪切光標(biāo)前的字符 |
------------------------------- | -------------------------------------------- |
Ctrl-l |
清除屏幕 |
Ctrl-c |
中止命令 |
Ctrl-d |
退出ipython會話 |
------------------------------------- | -------------------------------------------- |
Ctrl-p (or the up arrow key) |
進(jìn)入歷史的上一個命令 |
Ctrl-n (or the down arrow key) |
進(jìn)入歷史的下一個命令 |
Ctrl-r |
搜索命令歷史 |
魔術(shù)命令
代碼粘貼命令
有時候想要在其他地方復(fù)制一段代碼,可直接在命令行里粘貼會報錯榔至,就可以使用%paste
和 %cpaste
命令抵赢。
先在其他地方復(fù)制一段代碼,然后
In [3]: %paste
>>> def donothing(x):
... return x
## -- End pasted text --
PS:只需輸入%paste
%cpaste
在輸完之后留了一個交互空間唧取,可以像文本編輯器一樣輸入铅鲤。
代碼執(zhí)行命令
%run
可以用來執(zhí)行外部py腳本,函數(shù)枫弟,代碼邢享。
#-------------------------------------
# file: myscript.py
def square(x):
"""square a number"""
return x ** 2
for N in range(1, 4):
print(N, "squared is", square(N))
你可以在IPython會話中執(zhí)行:
In [6]: %run myscript.py
1 squared is 1
2 squared is 4
3 squared is 9
代碼計(jì)時命令
%timeit
統(tǒng)計(jì)一行代碼的執(zhí)行時間
In [8]: %timeit L = [n ** 2 for n in range(1000)]
1000 loops, best of 3: 325 μs per loop
```也可以使用``%%timeit``統(tǒng)計(jì)代碼塊的執(zhí)行時間。
```ipython
In [9]: %%timeit
...: L = []
...: for n in range(1000):
...: L.append(n ** 2)
...:
1000 loops, best of 3: 373 μs per loop
更多的魔術(shù)命令
%magic
%lsmagic
%timeit?
In [12]: %lsmagic
Available line magics:
%alias %alias_magic %autoawait %autocall %autoindent %automagic %bookmark %cd %cls %colors %config %copy %cpaste %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %killbgscripts %ldir %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %notebook %page %paste %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective
%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 %%cmd %%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.
In Out 的妙用
在ipython中的每個會話都有編號淡诗,就是In 骇塘,如果有輸出就有一個Out編號伊履。在整個過程可以直接調(diào)用。
In [4]: print(In)
In [5]: Out
In [6]: Out[2] ** 2 + Out[3] ** 2
#也可以用_來代替上一個輸出款违,__代表第上兩個輸出唐瀑。
In [7]: print(_)
In [8]: print(__)
In [9]: print(___)
#也可以判斷第4次輸入是否有輸出
In [10]: 14 in Out
#查看歷史命令
In [16]: %history -n 1-4
1: import math
2: math.sin(2)
3: math.cos(2)
4: print(In)
在IPython中執(zhí)行shell命令
所謂shell命令就是在系統(tǒng)中執(zhí)行的命令。
ipython可以直接執(zhí)行系統(tǒng)命令插爹,這是普通python環(huán)境所沒有的哄辣。
In [1]: !ls
myproject.txt
In [2]: !pwd
/home/jake/projects/myproject
In [3]: !echo "printing from the shell"
printing from the shell
也可以對命令的返回值賦值
In [4]: contents = !ls
In [5]: print(contents)
['myproject.txt']
In [6]: directory = !pwd
In [7]: print(directory)
['/Users/herui/notebooks/tmp/myproject']
也可以使用魔法命令代替
In [14]: %cd ..
/home/herui/projects
%cat %cp %env %ls %man %mkdir %more %rm %pwd等等
錯誤與調(diào)試
錯誤棧樣式
使用魔法命令%xmode
是exception mode的縮寫。它有三種模式赠尾。plain,Context,Verbose,默認(rèn)為Context.
修改模式:
%xmode Plain
調(diào)試:%debug
進(jìn)入錯誤環(huán)境力穗,然后可以自己調(diào)試。
Exception reporting mode: Plain
Automatic pdb calling has been turned ON
Traceback (most recent call last):
File "<ipython-input-9-569a67d2d312>", line 3, in <module>
func2(1)
File "<ipython-input-1-d849e34d61fb>", line 7, in func2
return func1(a, b)
File "<ipython-input-1-d849e34d61fb>", line 2, in func1
return a / b
ZeroDivisionError: division by zero
> <ipython-input-1-d849e34d61fb>(2)func1()
1 def func1(a, b):
----> 2 return a / b
3
#在這里:
ipdb> print(b)
0
ipdb> quit
時間和效率記錄
四個命令
%time
%timeit
%prun
==> program run
lprun
==> line program run
time 和 timeit 不再展示
def sum_of_lists(N):
total = 0
for i in range(5):
L = [j ^ (j >> i) for j in range(N)]
total += sum(L)
return total
%prun sum_of_lists(1000000)
%lprun -f sum_of_lists sum_of_lists(5000)
結(jié)果像這樣:
Timer unit: 1e-06 s
Total time: 0.009382 s
File: <ipython-input-19-fa2be176cc3e>
Function: sum_of_lists at line 1
Line # Hits Time Per Hit % Time Line Contents
==============================================================
1 def sum_of_lists(N):
2 1 2 2.0 0.0 total = 0
3 6 8 1.3 0.1 for i in range(5):
4 5 9001 1800.2 95.9 L = [j ^ (j >> i) for j in range(N)]
5 5 371 74.2 4.0 total += sum(L)
6 1 0 0.0 0.0 return total
更多內(nèi)容請查看官方文檔萍虽!