ipython
是一個升級版的交互式python命令行工具.
ipython安裝
pip install ipython
等到命令執行完成后顯示successfully
表示完裝成功
在命令提示符下輸入ipython
就可以啟動ipython了
其與原版python命令行工具不同在於ipython的提示符變成了in和out.
in
為輸入命令的地方,out
為命令執行完成后輸出的地方
ipython的特點
tab鍵自動補全一些常用的方法
支持一些系統命令
In [2]: pwd # 顯示當前所在目錄
Out[2]: '/root'
In [3]: cd .. # 返回當前目錄的上一級目錄
/
執行系統命令(!)
In [6]: !ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.81.10 netmask 255.255.255.0 broadcast 192.168.81.255
inet6 fe80::a545:8b99:d507:4d0f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:95:d5:31 txqueuelen 1000 (Ethernet)
RX packets 12851 bytes 9887304 (9.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7172 bytes 1546188 (1.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 140 bytes 12132 (11.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 140 bytes 12132 (11.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
In [7]: !ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:95:d5:31 brd ff:ff:ff:ff:ff:ff
inet 192.168.81.10/24 brd 192.168.81.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::a545:8b99:d507:4d0f/64 scope link
valid_lft forever preferred_lft forever
In [8]: !cat /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
?命令(內省,命令空間搜索)
In [12]: l1?
Type: list
String form: [1, 2, 3, 4]
Length: 4
Docstring:
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
In [13]: def func():
...: print("hello world")
...:
In [14]: func?
Signature: func()
Docstring: <no docstring>
File: /<ipython-input-13-4475a92670e6>
Type: function
In [15]: func?? # 打印函數的源碼
Signature: func()
Source:
def func():
print("hello world")
File: /<ipython-input-13-4475a92670e6>
Type: function
In [17]: l1.a*?
l1.append
In [18]: l1.p*?
l1.pop
In [19]: l1.__*__?
l1.__add__
l1.__class__
l1.__contains__
l1.__delattr__
l1.__delitem__
l1.__dir__
l1.__doc__
l1.__eq__
l1.__format__
l1.__ge__
l1.__getattribute__
l1.__getitem__
l1.__gt__
l1.__hash__
l1.__iadd__
l1.__imul__
l1.__init__
l1.__init_subclass__
l1.__iter__
l1.__le__
l1.__len__
l1.__lt__
l1.__mul__
l1.__ne__
l1.__new__
l1.__reduce__
l1.__reduce_ex__
l1.__repr__
l1.__reversed__
l1.__rmul__
l1.__setattr__
l1.__setitem__
l1.__sizeof__
l1.__str__
l1.__subclasshook__
%run命令執行文件代碼
In [29]: !vi test.py
In [30]: !cat test.py
def func1():
print("hello world")
func1()
In [31]: %run "test.py"
hello world
%paste和%cpaste命令執行剪帖板代碼
In [2]: %paste
def func1():
print("hello world")
func1()
## -- End pasted text --
hello world
與編輯器和IDE交互
魔術命令:%timeit %pdb
In [37]: %timeit a+b
47.1 ns ± 0.955 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
pdb是python debug的簡寫,一般用於排錯
使用命令歷史
使用上箭頭或下箭頭可以查看上一條命令或下一條命令的歷史
輸入與輸出變量
In [40]: !ls
1 boot etc lib media opt root sbin sys tmp var
bin dev home lib64 mnt proc run srv test.py usr
In [41]: _ # 執行前面倒數第一條命令
Out[41]: 3
In [47]: !ls
1 boot etc lib media opt root sbin sys tmp var
bin dev home lib64 mnt proc run srv test.py usr
In [48]: print("hello world")
hello world
In [49]: !ls
1 boot etc lib media opt root sbin sys tmp var
bin dev home lib64 mnt proc run srv test.py usr
In [50]: __
Out[50]: 3
In [54]: _i48 # 執行第48條命令
Out[54]: 'print("hello world")'
目錄書簽系統%bookmark
In [55]: %bookmark local /usr/local # 定義local書簽
In [56]: %bookmark selinux /etc/sysconfig/selinux # 定義selinux書簽
In [57]: %bookmark -l # 顯示所有的書簽
Current bookmarks:
local -> /usr/local
selinux -> /etc/sysconfig/selinux
In [55]: %bookmark local /usr/local
In [56]: %bookmark sysconfig /etc/sysconfig
In [57]: %bookmark -l
Current bookmarks:
local -> /usr/local
sysconfig -> /etc/sysconfig
In [58]: pwd
Out[58]: '/'
In [59]: cd local
(bookmark:local) -> /usr/local
/usr/local
In [60]: pwd
Out[60]: '/usr/local'
In [61]: cd sysconfig
(bookmark:sysconfig) -> /etc/sysconfig
/etc/sysconfig
In [62]: pwd
Out[62]: '/etc/sysconfig'
ipython notebook
安裝jupyter
pip install jupyter
運行界面
ipython常用的魔術命令
%quickref 顯示ipython的快速參考
%magic 顯示所有的魔術命令的詳細文檔
%debug 從最新的異常跟蹤的底部進入交互式調試器
%hist 打印命令的輸入(可選輸出)歷史
%pdb 在異常發生后自動進入調試器
%paste 執行剪貼板中的python代碼
%cpaste 打開一個特殊提示符以便手工粘貼待執行的python代碼
%reset 刪除interactive命名空間中的全部變量/名稱
%page OBJECT 通過分頁器打印輸出object
%run script.py 在ipython中執行一個python腳本文件
%prun statement 通過cprofile執行statement,並打印分析器的輸出結果
%time statement 報告statement的執行時間
%timeit statement 多次執行statement以計算系統平均執行時間.對那么執行時間非常小的代碼很有用
%who,%who_id,%whos 顯示interactive命名空間中定義的變量,信息級別/冗余度可變
%xdel variable 刪除variable,並嘗試清除其在ipython中的對象上的一切引用
python調試器命令
h(help) 顯示命令列表
help command 顯示command的文檔
c(continue) 恢復程序的執行
q(quit) 退出調試器,不再執行任何代碼
b(break) n 在當前文件的第n行設置一個斷點
b path/to/file.py:n 在指定文件的第n行設置一個斷點
s(step) 單步進入函數調用
n(next) 執行當前行,並前進到當前級別的下一行
u(up)/d(down) 在函數調用棧中向上或向下移動
a(args) 顯示當前函數的參數
debug statement 在新的遞歸調試器中調用語句statement
l(list) statement 顯示當前行,以及當前棧級別上的上下文參考代碼
w(where) 打印當前位置的完整棧跟蹤(包括上下文參考代碼)
ipython快捷鍵
Ctrl+p或者向上鍵頭 向后搜索命令歷史中以當前輸入的文本開頭的命令
Ctrl+n或者向上鍵頭 向前搜索命令歷史中以當前輸入的文本開頭的命令
Ctrl+r 按行讀取的反向歷史搜索(部分匹配)
Ctrl+Shift+variable 從剪貼板粘貼文本
Ctrl+c 中止當前正在執行的代碼
Ctrl+a 把光標移動到行首
Ctrl+e 把光標移動到行尾
Ctrl+k 刪除從光標開始到行尾的文本
Ctrl+u 清除當前行的所有內容
Ctrl+f 將光標向前移動一個字符
Ctrl+b 將光標向后移動一個字符
Ctrl+l 清屏