设为首页 收藏本站
查看: 843|回复: 0

[经验分享] python tab键自动补齐命令

[复制链接]

尚未签到

发表于 2018-8-9 08:09:09 | 显示全部楼层 |阅读模式
我的博客已迁移到xdoujiang.com请去那边和我交流  
一、基础环境
  
1、角色、ip、版本、内核
  
serverA 10.1.10.117 3.2.0-4-amd64 7.8 python readline rlcompleter
  
python-2.7.3
  

  
二、python tab键自动补齐命令安装
  
1、安装python
  
apt-get -y install python
  

  
2、查看下目前已安装的模块
  
python
  
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
  
[GCC 4.7.2] on linux2
  
Type "help", "copyright", "credits" or "license" for more information.
  
>>> help('modules')
  
Please wait a moment while I gather a list of all available modules...
  
BaseHTTPServer      array               imaplib             sha
  
Bastion             ast                 imghdr              shelve
  
CDROM               asynchat            imp                 shlex
  
CGIHTTPServer       asyncore            importlib           shutil
  
Canvas              atexit              imputil             signal
  
ConfigParser        audiodev            inspect             site
  
Cookie              audioop             io                  sitecustomize
  
DLFCN               base64              itertools           smtpd
  
Dialog              bdb                 json                smtplib
  
DocXMLRPCServer     binascii            keyword             sndhdr
  
FileDialog          binhex              lib2to3             socket
  
FixTk               bisect              linecache           spwd
  
HTMLParser          bsddb               linuxaudiodev       sqlite3
  
IN                  bz2                 locale              sre
  
MimeWriter          cPickle             logging             sre_compile
  
Queue               cProfile            macpath             sre_constants
  
ScrolledText        cStringIO           macurl2path         sre_parse
  
SimpleDialog        calendar            mailbox             ssl
  
SimpleHTTPServer    cgi                 mailcap             stat
  
SimpleXMLRPCServer  cgitb               markupbase          statvfs
  
SocketServer        chunk               marshal             string
  
StringIO            cmath               math                stringold
  
TYPES               cmd                 md5                 stringprep
  
Tix                 code                mhlib               strop
  
Tkconstants         codecs              mimetools           struct
  
Tkdnd               codeop              mimetypes           subprocess
  
Tkinter             collections         mimify              sunau
  
UserDict            colorsys            mmap                sunaudio
  
UserList            commands            modulefinder        symbol
  
UserString          compileall          multifile           symtable
  
_LWPCookieJar       compiler            multiprocessing     sys
  
_MozillaCookieJar   contextlib          mutex               sysconfig
  
__builtin__         cookielib           netrc               syslog
  
__future__          copy                new                 tabnanny
  
_abcoll             copy_reg            nis                 tarfile
  
_ast                crypt               nntplib             telnetlib
  
_bisect             csv                 ntpath              tempfile
  
_bsddb              ctypes              nturl2path          termios
  
_codecs             curses              numbers             test
  
_codecs_cn          datetime            opcode              textwrap
  
_codecs_hk          dbhash              operator            this
  
_codecs_iso2022     dbm                 optparse            thread
  
_codecs_jp          debconf             os                  threading
  
_codecs_kr          decimal             os2emxpath          time
  
_codecs_tw          difflib             ossaudiodev         timeit
  
_collections        dircache            parser              tkColorChooser
  
_csv                dis                 pdb                 tkCommonDialog
  
_ctypes             distutils           pickle              tkFileDialog
  
_ctypes_test        doctest             pickletools         tkFont
  
_curses             dumbdbm             pipes               tkMessageBox
  
_curses_panel       dummy_thread        pkgutil             tkSimpleDialog
  
_elementtree        dummy_threading     platform            toaiff
  
_functools          email               plistlib            token
  
_hashlib            encodings           popen2              tokenize
  
_heapq              errno               poplib              trace
  
_hotshot            exceptions          posix               traceback
  
_io                 fcntl               posixfile           ttk
  
_json               filecmp             posixpath           tty
  
_locale             fileinput           pprint              turtle
  
_lsprof             fnmatch             profile             types
  
_multibytecodec     formatter           pstats              unicodedata
  
_multiprocessing    fpectl              pty                 unittest
  
_pyio               fpformat            pwd                 urllib
  
_random             fractions           py_compile          urllib2
  
_socket             ftplib              pyclbr              urlparse
  
_sqlite3            functools           pydoc               user
  
_sre                future_builtins     pydoc_data          uu
  
_ssl                gc                  pyexpat             uuid
  
_strptime           genericpath         quopri              warnings
  
_struct             getopt              random              wave
  
_symtable           getpass             re                  weakref
  
_sysconfigdata      gettext             readline            webbrowser
  
_sysconfigdata_nd   glob                repr                whichdb
  
_testcapi           grp                 resource            wsgiref
  
_threading_local    gzip                rexec               xdrlib
  
_warnings           hashlib             rfc822              xml
  
_weakref            heapq               rlcompleter         xmllib
  
_weakrefset         hmac                robotparser         xmlrpclib
  
abc                 hotshot             runpy               xxsubtype
  
aifc                htmlentitydefs      sched               zipfile
  
antigravity         htmllib             select              zipimport
  
anydbm              httplib             sets                zlib
  
argparse            ihooks              sgmllib
  
Enter any module name to get more help.  Or, type "modules spam" to search
  
for modules whose descriptions contain the word "spam".
  

  
3、需要用到模块说明rlcompleter readline
  
rlcompleter:
  
The rlcompleter module defines a completion function suitable for the readline
  
module by completing valid Python identifiers and keywords.
  
When this module is imported on a Unix platform with the readline module available
  
an instance of the Completer class is automatically created and its complete() method is set as the readline completer.
  
readline:
  
The readline module defines a number of functions to facilitate completion and reading/writing of
  
history files from the Python interpreter.This module can be used directly or via the rlcompleter module.
  
Settings made usingthis module affect the behaviour of both the interpreter interactive
  
prompt and the prompts offered by the raw_input() and input() built-in functions.
  

  
4、具体脚本
  
cat .pythonrc.py
  
#!/usr/bin/python
  
# -*- coding: utf-8 -*-
  
#--------------------------------------------------
  
#Author:jimmygong
  
#Email:jimmygong@taomee.com
  
#FileName:.pythonrc.py
  
#Function:
  
#Version:1.0
  
#Created:2015-10-12
  
#--------------------------------------------------
  
print "success set"
  
try:
  
    import readline
  
except ImportError:
  
    print "Module readline not available."
  
else:
  
    import rlcompleter
  
    readline.parse_and_bind("tab: complete")
  

  
5、执行后会看到相应success set出现说明OK了
  
python .pythonrc.py
  
success set
  

  
6、将pythonrc.py脚本放到.bashrc
  
echo "export PYTHONSTARTUP=~/.pythonrc.py" >> .bashrc
  

  
7、生效
  
source .bashrc
  

  
三、参考文章
  
https://docs.python.org/2/library/rlcompleter.html
  
https://docs.python.org/2/library/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-548880-1-1.html 上篇帖子: 工位上的Python——makefile函数 下篇帖子: python之read,readline,readlines区别
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表