Python's Way of customizing the interactive command line _python

Source: Internet
Author: User
Tags md5 readline

The interactive command line for Python can be configured with a startup file.

When Python starts, it looks for the environment variable Pythonstartup and executes the program code in the file specified in the variable. The specified file name and address can be arbitrary. When you press the TAB key, the full content and command history are automatically filled. This effectively enhances the command line, and these tools are implemented based on the ReadLine module (which requires a ReadLine library-assisted implementation).

Here's a simple example of a startup script file that adds a button AutoComplete and history command function to the Python command line.

[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

Setting environment variables

[Python@python ~]$ cat. Bash_profile|grep python
export Pythonstartup=/home/python/.pythonstartup

Verify tab completion and History command view.

[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 the 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

Note: If you appear at make:

Python build finished, but the necessary bits to build this modules were not found:
_tkinter gdbm readline      Sunaudiodev

If you ignore this, import ReadLine will make an error. Indicates no module specified!

Here is the missing specified package:

Redhat:   readline-devel.xxx.rpm

The problem can be resolved by recompiling execution on the installation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.