Centos上python解释器按上下键或退格键出现乱码解决和tab补全



出现此问题主要是由于未安装readline,可以使用python自带的readline,具体设置方式为:

1、cd /Python-2.7.9 (下载包后的路径)

2、./configure

3、vim /Python-2.7.9/Modules/Setup


取消前面的注释

4、make&&make install



下载安装readline


#wget https://pypi.python.org/packages/source/r/readline/readline-6.2.4.1.tar.gz#md5=578237939c81fdbc2c8334d168b17907 --no-check-certificate
#tar -zxvf readline-6.2.4.1.tar.gz
#cd readline-6.2.4.1
#python setup.py install





这样就把python自动补全的功能安装完毕,下面是导入tab

import readline, rlcompleter; readline.parse_and_bind("tab: complete")


或者把tab写出脚本,以模块的方式导入(推荐此方法)



tab补全

编写一个Tab键自动补全功能的脚本。

vim tab.py


#!/usr/bin/python 
# python startup file 
import sys 
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



使用方法

查看python默认的模块存放路径。

python

import sys

sys.path


默认的是这个:/usr/lib/python2.6


拷贝功能脚本到默认模块存放路径。

cp tab.py /usr/lib/python2.6


import tab  不出错就是ok

按tab键会出来一些东西