python定製交互式命令行

   有時候用python處理一些簡單的事務,爲此打開編輯器編輯一個可執行的py文件保存執行就顯得得不償失了。這時,可以定製一下python提供的交互式命令行來實現Tab補全和歷史命令補全。當然,你也可以簡單的安裝ipython實現上述功能。

   實現方法:

   1.在家目錄下編輯.pythonstartup,內容如下(可能需要安裝python的readline模塊)

   

# python startup
import readline
import rlcompleter
import atexit
import os
# tab compltion
readline.parse_and_bind('tab: complete')
# history file
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
    readline.read_history_file(histfile)
except IOError:
    pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter

   2.在.bash_profile文件添加如下行

export PYTHONSTARTUP=~/.pythonstartup

   3.重讀.bash_profile

   

. .bash_profile

   好了,現在你就有了一個功能增強的py命令行

161645514.jpg

本文出自 “不材神木” 博客,請務必保留此出處http://ouroboros.blog.51cto.com/2468486/1275068

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章