python简介

支持的系统:跨平台
优点:开发效率高
缺点:执行速度慢
应用面:网站开发、自动化运维、游戏开发、爬虫、数据分析、人工智能

实验环境阿里云、腾讯云

查看python版本

[root@Tencent ~]# python
Python 2.7.5 (default, Aug  7 2019, 00:51:29) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

阿里云、腾讯云系统默认python版本为2.7,由于python官方宣布 Python 2 将于 2020 年停用,所以直接升级python

1、下载python安装包
[root@Tencent ~]#wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tar.xz

2、解压
[root@Tencent ~]# tar -xf Python-3.5.0.tar.xz 

3、编译安装
[root@Tencent ~]# cd Python-3.5.0/
[root@Tencent Python-3.5.0]# ./configure --prefix=/usr/local/python3
[root@Tencent Python-3.5.0]# make && make install

4、备份python2.7
[root@Tencent Python-3.5.0]# mv /usr/bin/python /usr/bin/python2.7.bak

5、给python3创建软连接
[root@Tencent Python-3.5.0]# ln -sv /usr/local/python3/bin/python3.5 /usr/bin/python
?.usr/bin/python?.-> ?.usr/local/python3/bin/python3.5?

6、查看python版本及退格键和方向键使用
[root@Tencent Python-3.5.0]# python
Python 3.5.0 (default, Feb 11 2020, 13:26:01) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

若发现python3退格和方向键无法正常使用,请安装readline库文件,由于使用的是云服务器,所以配置yum源略,可参考 https://blog.51cto.com/14015577/2414577

7、安装readline库文件
[root@VM_0_4_centos ~]# yum install readline-devel.* -y

此时发现yum不能用,需要修改/usr/bin/yum和/usr/libexec/urlgrabber-ext-down的python的路径为系统自带版本,例如/usr/bin/python2.7

8、修改yum的解释器
[root@Tencent ~]# head -n 1 /usr/bin/yum
#!/usr/bin/python2.7
[root@Tencent ~]]# head -n 1 /usr/libexec/urlgrabber-ext-down 
#! /usr/bin/python2.7

安装readline库文件
[root@Tencent ~]yum install readline-devel.* -y

9、安装readline库成功,重新安装编译python
[root@Tencent Python-3.5.0]# ./configure --prefix=/usr/local/python3
[root@Tencent Python-3.5.0]# make && make install

10、给python3创建软连接
[root@Tencent Python-3.5.0]# ln -sv /usr/local/python3/bin/python3.5 /usr/bin/python
?.usr/bin/python?.-> ?.usr/local/python3/bin/python3.5?

python退格和方向键可以正常使用

11、查看python版本及退格键和方向键使用
[root@Tencent Python-3.5.0]# python
Python 3.5.0 (default, Feb 11 2020, 13:26:01) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

因为yum采用python作为解释器,,给python3 添加alias别名,这样不用更改yum的解释器,直接在第
3步编译安装完成后操作就行。此时python3使用别名即可,退格键和方向键正常使用。此方法可省略步骤3后所有步骤。

临时更改    
[root@Tencent Python-3.5.0]# alias python3=/usr/local/python3/bin/python3.5
永久更改
[root@Tencent Python-3.5.0]#vim ~/.bashrc 
alias python3=/usr/local/python3/bin/python3.5

[root@Tencent Python-3.5.0]# python
Python 2.7.5 (default, Aug  7 2019, 00:51:29) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

[root@VM_0_4_centos ~]# python3
Python 3.5.0 (default, Feb 11 2020, 14:11:53) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello world!')
hello world!