- ipython中可以使用edit命令編輯文件內(nèi)容轩娶,edit打開文件的方式根據(jù)系統(tǒng)已安裝編輯器決定,也可以通過執(zhí)行ipython前使用
export EDITOR=vim
自己指定編輯器類型框往。
比如:
edit t.py
輸入:
l = range(10)
- 通過run命令可以執(zhí)行python腳本:
run t.py
--------------------
In [8]: l
Out[8]: range(0, 10)
可以看到鳄抒,執(zhí)行完t.py這個(gè)腳本后,l就可以被識別到了椰弊。
- 使用alias設(shè)置常用命令別名:
alias largest ls -1sSh | grep %s
%largest t
要存儲別名许溅,需要使用
store largest
- 使用bookmark存儲書簽,相當(dāng)于linux下的軟鏈接秉版。
%bookmark root /home
cd root //相當(dāng)于進(jìn)入了home目錄
- hist命令查看歷史紀(jì)錄贤重,并使用save命令存儲指定行的內(nèi)容到文件中
save a.py 9-11 //存儲9-11行到a.py這個(gè)文件中
- 使用debug函數(shù)進(jìn)入調(diào)試器調(diào)試程序:
In [18]: a = 1
In [19]: b = 0
In [20]: a / b
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-20-d8e10abd5ab6> in <module>()
----> 1 a / b
ZeroDivisionError: division by zero
In [21]: debug
> <ipython-input-20-d8e10abd5ab6>(1)<module>()
----> 1 a / b
ipdb> p a
1
ipdb> p b
0
ipdb> exit
- 使用%rehashx命令將linux下的命令都更新進(jìn)ipython中方便實(shí)用:
In [23]: echo 1
File "<ipython-input-23-932d3634cbf0>", line 1
echo 1
^
SyntaxError: invalid syntax
In [24]: %rehashx
In [25]: echo 1
1
使用%store -r恢復(fù)之前保存的別名
使用load命令只加載程序內(nèi)容,不執(zhí)行清焕,跟run有區(qū)別并蝗,run直接執(zhí)行。
使用logstart可以將ipython中的內(nèi)容記錄為日志文件保存起來秸妥。
In [26]: logstart
Activating auto-logging. Current session state plus future input saved.
Filename : ipython_log.py
Mode : rotate
Output logging : False
Raw input log : False
Timestamping : False
State : active
In [27]: a = 1
In [28]: b =2
In [29]: c= 1
In [30]: %logoff
Switching logging OFF
之后推出ipython后再次啟動ipython之前使用:
ipython -i ipython_log.py
就可以將之前記錄的變量以及數(shù)據(jù)繼續(xù)使用滚停。
- 編輯ipython配置文件實(shí)現(xiàn)autoreload功能
默認(rèn)是沒有ipython_config.py這個(gè)配置文件的,需要使用 ipython profile create 這個(gè)命令創(chuàng)建一個(gè)默認(rèn)配置文件
編輯.ipython/profile_default/ipython_config.py
將
32:c.InteractiveShellApp.exec_lines = ['autoreload 2']
35:c.InteractiveShellApp.extensions = ['autoreload']
600:c.StoreMagics.autorestore = True
分別改為上述內(nèi)容
再次進(jìn)入ipython:
In [1]: edit test.py
Editing... done. Executing edited code...
-----------------------------------------
文件test.py的內(nèi)容:
def run():
return 2
-----------------------------------------
In [2]: from test import run
In [3]: run()
Out[3]: 2
In [4]: edit test.py
Editing... done. Executing edited code...
-----------------------------------------
文件test.py的內(nèi)容:
def run():
return 10
-----------------------------------------
In [5]: run()
Out[5]: 10
可以看到我們手動修改了test.py中的run函數(shù)返回的值后不需要再次import這個(gè)run函數(shù)就可以自動返回最新的值(10)粥惧,這就是autoreload的作用键畴。
2018.6.21 北京 晴 天微亮 失眠之夜寫下這個(gè)筆記