python實現(xiàn)定制交互式命令行的方法
Python的交互式命令行可通過啟動文件來配置。
當Python啟動時,會查找環(huán)境變量PYTHONSTARTUP,并且執(zhí)行該變量中所指定文件里的程序代碼。該指定文件名稱以及地址可以是隨意的。按Tab鍵時會自動補全內(nèi)容和命令歷史。這對命令行的有效增強,而這些工具則是基于readline模塊實現(xiàn)的(這需要readline程序庫輔助實現(xiàn))。
此處為大家舉一個簡單的啟動腳本文件例子,它為python命令行添加了按鍵自動補全內(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
驗證Tab鍵補全和歷史命令查看。
[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的時候出現(xiàn):
Python build finished, but the necessary bits to build these modules were not found: _tkinter gdbm readline sunaudiodev
如果對此忽略了的話,import readline會報錯。表示沒有指定模塊!
這里是缺少指定包:
redhat: readline-devel.xxx.rpm
安裝上重新編譯執(zhí)行,問題即可得到解決。
相關(guān)文章
python3.7.3版本和django2.2.3版本是否可以兼容
在本篇文章里小編給大家整理的是一篇關(guān)于python3.7.3版本和django2.2.3版本是否可以兼容的相關(guān)知識點內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-09-09
使用python將圖片格式轉(zhuǎn)換為ico格式的示例
今天小編就為大家分享一篇使用python將圖片格式轉(zhuǎn)換為ico格式的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10

