centos7安装python3 以及tab补全功能
1.安装python3
1.1下载python源码包
网址:https://www.python.org/downloads/release/python-362/
下载地址:https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
1.1.1安装python前的库环境,非常重要
yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y
1.2下载,解压安装源码包
1、./configure 是用来检测你的安装平台的目标特征的。比如它会检测你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本。 2、make 是用来编译的,它从Makefile中读取指令,然后编译。 3、make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置。
wget https://www.python.org/ftp/python/3.4.7/Python-3.4.7.tar.xz
xz -d Python-3.4.7.tar.xz
tar -xf Python-3.4.7.tar
#yum install gcc -y
./configure --prefix=/opt/python make && make install
---------------------
用python查看默认安装路径
>>> import sys
>>> sys.path
['', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6',
'/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']
--------------------
python3.4默认没添加path
在/etc/profile最后一行添加
export PATH=$PATH:/opt/python/bin
然后
source /etc/profile
#python快捷path就被改了,yum用的python2,麻烦
1.2.3 牛逼的python Tab自动补全
下载setuptool 3.4以上已经默认安装了easy_install-3.4
下载地址: https://pypi.python.org/pypi/setuptools/36.2.7 下载命令: wget https://pypi.python.org/packages/07/a0/11d3d76df54b9701c0f7bf23ea9b00c61c5e14eb7962bb29aed866a5844e/setuptools-36.2.7.zip#md5=b9e6c049617bac0f9e908a41ab4a29ac 解压: unzip setuptools-36.2.7.zip 编译: python3 setup.py build python2 setup.py build 无误后安装: python3 setup.py install python2 setup.py install 没什么用
下载readline
#yum install -y readline-devel -y #pip install readline
tab补全
#python startup file
import readline,rlcompleter
#tab completion
readline.parse_and_bind('tab:complete')
----------------------------
python历史记录
#history file
import os
history_file = os.path.join(os.environ["HOME"],".pythonhistory")
[root@mm_local ~]# echo "export PYTHONSTARTUP='/root/.pythonstartup'" >> /root/.bashrc [root@mm_local ~]# source /root/.bashrc [root@mm_local ~]# cat /root/.pythonstartup import os import readline import rlcompleter import atexit #tab completion readline.parse_and_bind("tab: complete") #history file history_file = os.path.join(os.environ["HOME"],".pythonhistory") try: readline.read_history_file(history_file) except IOError: pass atexit.register(readline.write_history_file,history_file) del os,history_file,readline,rlcompleter [root@mm_local ~]# python Python 2.7.3 (default, Jun 5 2013, 22:40:26) [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path<tab> #按tab键就可以自动补全了 [root@mm_local ~]# python >>> >>> sys.path #按上下方向键即可找到历史命令 整体代码
加入python的启动环境变量,永久生效自动补全
只需要启动时执行一条命令即可 import rlcompleter, readline readline.parse_and_bind('tab:complete') 可以将上述的代码存于~/.pythonrc.py文件中。 然后,在~/.bashrc中设置 export PYTHONSTARTUP=~/.pythonrc.py 这样,每次启动终端,bash就会设置PYTHONSTARTUP这个环境变量,然后,python启动时就会使用这个变量指定的文件中的初始化代码。 在python解释器中,输入一个变量的一部分,或者变量后面加句点,再按TAB键就可以看到自动补全的威力。