python實(shí)現(xiàn)定制交互式命令行的方法
Python的交互式命令行可通過啟動(dòng)文件來配置。
當(dāng)Python啟動(dòng)時(shí),會(huì)查找環(huán)境變量PYTHONSTARTUP,并且執(zhí)行該變量中所指定文件里的程序代碼。該指定文件名稱以及地址可以是隨意的。按Tab鍵時(shí)會(huì)自動(dòng)補(bǔ)全內(nèi)容和命令歷史。這對(duì)命令行的有效增強(qiáng),而這些工具則是基于readline模塊實(shí)現(xiàn)的(這需要readline程序庫輔助實(shí)現(xiàn))。
此處為大家舉一個(gè)簡單的啟動(dòng)腳本文件例子,它為python命令行添加了按鍵自動(dòng)補(bǔ)全內(nèi)容和歷史命令功能。
[python@python ~]$ cat .pythonstartup
import readline
import rlcompleter
import atexit
import os
#tab completion
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
設(shè)置環(huán)境變量
[python@python ~]$ cat .bash_profile|grep PYTHON export PYTHONSTARTUP=/home/python/.pythonstartup
驗(yàn)證Tab鍵補(bǔ)全和歷史命令查看。
[python@python ~]$ python Python 2.7.5 (default, Oct 6 2013, 10:45:13) [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import md5 >>> md5. md5.__class__( md5.__getattribute__( md5.__reduce__( md5.__subclasshook__( md5.__delattr__( md5.__hash__( md5.__reduce_ex__( md5.blocksize md5.__dict__ md5.__init__( md5.__repr__( md5.digest_size md5.__doc__ md5.__name__ md5.__setattr__( md5.md5( md5.__file__ md5.__new__( md5.__sizeof__( md5.new( md5.__format__( md5.__package__ md5.__str__( md5.warnings >>> import os >>> import md5
注意:如果在make的時(shí)候出現(xiàn):
Python build finished, but the necessary bits to build these modules were not found: _tkinter gdbm readline sunaudiodev
如果對(duì)此忽略了的話,import readline會(huì)報(bào)錯(cuò)。表示沒有指定模塊!
這里是缺少指定包:
redhat: readline-devel.xxx.rpm
安裝上重新編譯執(zhí)行,問題即可得到解決。
相關(guān)文章
python3.7.3版本和django2.2.3版本是否可以兼容
在本篇文章里小編給大家整理的是一篇關(guān)于python3.7.3版本和django2.2.3版本是否可以兼容的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-09-09
20個(gè)超實(shí)用Python自動(dòng)化腳本分享
在當(dāng)今的快節(jié)奏工作環(huán)境中,自動(dòng)化不再是一種奢侈,而是提高效率和精確性的必需手段,這篇文章為大家整理了20個(gè)超實(shí)用Python自動(dòng)化腳本,希望對(duì)大家有所幫助2024-01-01
使用python將圖片格式轉(zhuǎn)換為ico格式的示例
今天小編就為大家分享一篇使用python將圖片格式轉(zhuǎn)換為ico格式的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10

