Centos7 6下安裝Python3 7

2022-09-23 08:56:59 字數 3320 閱讀 1177

[[email protected] ~]# cat /etc/redhat-release 

centos linux release 7.6.1810 (core)

[[email protected] ~]# cd /etc/yum.repos.d/

[[email protected] yum.repos.d]# mkdir bak

[[email protected] yum.repos.d]# mv * bak

[[email protected] yum.repos.d]# wget

[[email protected] yum.repos.d]# wget

[[email protected] ~]# 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

[[email protected] ~]# yum -y groupinstall "development tools"

不安裝也有可能不會報錯,但是安裝了一定不會報錯,安不安裝自己把握。

[[email protected] ~]# cd /usr/local/src/

[[email protected] src]# wget

[[email protected] src]# tar xvf python-3.7.0.tar.xz

[[email protected] src]# mv python-3.7.0 /usr/local/python-3.7

[[email protected] src]# cd /usr/local/python-3.7/

[[email protected] python-3.7]# ./configure --prefix=/usr/local/sbin/python-3.7
將python3.7安裝到/usr/local/sbin/python-3.7目錄下

[[email protected] python-3.7]# make && make install
安裝完成後,若出現以下兩行,說明安裝成功;

installing collected packages: setuptools, pip

successfully installed pip-10.0.1 setuptools-39.0.1

[[email protected] python-3.7]# /usr/local/sbin/python-3.7/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

>>>

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

雖然能用了,但是命令較長使用起來並不方便,we konw 系統本身是帶的有python的,看一下版本

[[email protected] ~]# python -v

python 2.7.5

[[email protected] ~]# which python

/usr/bin/python

[[email protected] ~]# ll /usr/bin/ |grep python

-rwxr-xr-x. 1 root root 11312 nov 14 00:00 abrt-action-analyze-python

lrwxrwxrwx. 1 root root 7 mar 14 18:19 python -> python2

lrwxrwxrwx. 1 root root 9 mar 14 18:19 python2 -> python2.7

-rwxr-xr-x. 1 root root 7216 oct 31 07:46 python2.7

可以看到python命令是通過軟連結連線到python2.7的,[做運維的小年輕]那我們現在安裝了python3.7,是不是可以將python命令做一個軟連結連結到python3,答案是肯定的昂。

[[email protected] ~]# rm -rf /usr/bin/python

[[email protected] ~]# ln -sv /usr/local/sbin/python-3.7/bin/python3 /usr/bin/python

‘/usr/bin/python’ -> ‘/usr/local/sbin/python-3.7/bin/python3’

[[email protected] ~]# ll /usr/bin/ |grep python

-rwxr-xr-x. 1 root root 11312 nov 14 00:00 abrt-action-analyze-python

lrwxrwxrwx. 1 root root 38 mar 14 19:24 python -> /usr/local/sbin/python-3.7/bin/python3

lrwxrwxrwx. 1 root root 9 mar 14 18:19 python2 -> python2.7

-rwxr-xr-x. 1 root root 7216 oct 31 07:46 python2.7

[[email protected] ~]# python -v

python 3.7.0

①將/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

[[email protected] ~]# ln -s /usr/local/sbin/python-3.7/bin/pip3 /usr/bin/pip

[[email protected] ~]# pip --version

pip 10.0.1 from /usr/local/sbin/python-3.7/lib/python3.7/site-packages/pip (python 3.7)

大功告成。