Centos7.5 下安装Python3.7

1.首先来看一下系统版本

[root@python3 ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core)

2.安装python3.7之前需要先安装一些依赖,,这也是上面更新yum源的原因

[root@python3 ~]# yum -y install zlib-devel bzip2-devel openssl-devel openssl-static ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel lzma gcc

[root@python3 ~]# yum -y groupinstall "Development tools"

不安装也有可能不会报错,但是安装了一定不会报错,安不安装自己把握。

3.下载python3.7.5安装包,方法很多,我这里直接用wget从官网上下载

[root@python3 ~]# cd /usr/local/src/

[root@python3 src]# wget https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tar.xz

将安装包下载到/usr/local/src/目录下

4.解压&&移动

[root@python3 src]# tar xvf Python3.7.5.tar.xz

[root@python3 src]# mv Python3.7.5 /usr/local/python3.7.5

[root@python3 src]# cd /usr/local/python3.7.5/

5.安装&&编译

[root@python3 python-3.7]# ./configure --prefix=/usr/local/sbin/python3.7.5

将python3.7安装到/usr/local/sbin/python-3.7目录下

[root@python3 python-3.7]# make && make install

安装完成后,若出现以下两行,说明安装成功;

Installing collected packages: setuptools, pip
Successfully installed pip-10.0.1 setuptools-39.0.1

6.安装成功之后就能使用了

[root@python3 python-3.7]# /usr/local/sbin/python3.7.5/bin/python3
Python 3.7.0 (default, Mar 15 2019, 00:44:37) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

【此处退出的命令 exit()】

7.看一下python命令的绝对路径

➜ softSource ll /usr/bin/ |grep python
lrwxrwxrwx 1 root root 9 10月 28 23:12 python2 -> python2.7
-rwxr-xr-x 1 root root 7.1K 8月 7 08:52 python2.7
-rwxr-xr-x 1 root root 1.8K 8月 7 08:51 python2.7-config
lrwxrwxrwx 1 root root 16 10月 28 23:12 python2-config -> python2.7-config
lrwxrwxrwx 1 root root 24 11月 15 15:24 python3.6 -> /usr/local/bin/python3.6
lrwxrwxrwx 1 root root 40 11月 11 15:38 python3.7 -> /home/softSource/python3.5/lib/python3.7
lrwxrwxrwx 1 root root 18 11月 15 14:49 python.backup -> /usr/bin/python2.x
lrwxrwxrwx 1 root root 14 10月 28 23:12 python-config -> python2-config

以下内容根据情况使用

8.将python命令通过软链接指向到python3命令

[root@python3 ~]# ll /usr/bin/ |grep python
[root@python3 ~]#ln -s /usr/local/sbin/python.3.7.5/bin/python3 /usr/bin/python3.7.5

9.查看现在python3.7.5的版本

[root@python3 ~]# python3.7.5 -V
Python 3.7.5

10. (选择性谨慎修改)修改完python默认版本之后,会存不能执行yum命令,需要做一些修改,如下:

①将/usr/bin/yum的顶部的:

!/usr/bin/python  改成  !/usr/bin/python2.7 

②将/usr/libexec/urlgrabber-ext-down的顶部的:

/usr/bin/python  改为   /usr/bin/python2.7

③将/usr/bin/yum-config-manager的顶部的

#!/usr/bin/python 改为 #!/usr/bin/python2.7

11. (选择性谨慎修改)最后将pip指向到python3.7

[root@python3 ~]# ln -s /usr/local/sbin/python3.7.5/bin/pip3 /usr/bin/pip3

9.查看现在的pip3版本

[root@python3 ~]# pip3 --version
pip 19.2.3 from /usr/local/sbin/python.3.7.5/lib/python3.7/site-packages/pip (python 3.7)

发表评论

关闭菜单