Centos7.6下安裝Python3.7

前言

話說不會開發的運維不是一個好的DBA,因此我要開始學習python了,寫博客記錄一下個人學習過程,另外別欺負我新來的,那個天天更博的技術流ken是我哥。python

不說了,時間寶貴,開整。linux

1.首先來看一下系統版本

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

2.更新一下yum源,yum源我這裏用的都是阿里雲的源

複製代碼
[root@python3 ~]# cd /etc/yum.repos.d/

[root@python3 yum.repos.d]# mkdir bak

[root@python3 yum.repos.d]# mv * bak

[root@python3 yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo

[root@python3 yum.repos.d]# wget http://mirrors.aliyun.com/repo/epel-7.repo複製代碼
複製代碼

3.安裝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"複製代碼

不安裝也有可能不會報錯,可是安裝了必定不會報錯,安不安裝本身把握。sql

4.下載python3.7安裝包,方法不少,我這裏直接用wget從官網上下載

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

[root@python3 src]# wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz複製代碼

將安裝包下載到/usr/local/src/目錄下bash

5.解壓&&移動

[root@python3 src]# tar xvf Python-3.7.0.tar.xz

[root@python3 src]# mv Python-3.7.0 /usr/local/python-3.7

[root@python3 src]# cd /usr/local/python-3.7/複製代碼

6.安裝&&編譯

[root@python3 python-3.7]# ./configure --prefix=/usr/local/sbin/python-3.7複製代碼

將python3.7安裝到/usr/local/sbin/python-3.7目錄下運維

[root@python3 python-3.7]# make && make install複製代碼

安裝完成後,若出現如下兩行,說明安裝成功;python2.7

Installing collected packages: setuptools, pip
Successfully installed pip-10.0.1 setuptools-39.0.1複製代碼

7.安裝成功以後就能使用了

複製代碼
[root@python3 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
Type "help", "copyright", "credits" or "license" for more information.
>>>

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

雖然能用了,可是命令較長使用起來並不方便,we konw 系統自己是帶的有python的,看一下版本複製代碼

[root@python3 ~]# python -V工具

Python 2.7.5學習

複製代碼

8.看一下python命令的絕對路徑

複製代碼
[root@python3 ~]# which python

/usr/bin/python

[root@python3 ~]# 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,答案是確定的昂。ui

9.將python命令經過軟連接指向到python3命令(就是上面那一大串/usr/local/sbin/python-3.7/bin/python3)

複製代碼
[root@python3 ~]# rm -rf /usr/bin/python

[root@python3 ~]# ln -sv /usr/local/sbin/python-3.7/bin/python3 /usr/bin/python

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

[root@python3 ~]# 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複製代碼
複製代碼

10.查看如今的默認版本

[root@python3 ~]# python -V
Python 3.7.0複製代碼

11.修改完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 複製代碼
複製代碼

12.最後將pip指向到python3.7

[root@python3 ~]# ln -s /usr/local/sbin/python-3.7/bin/pip3 /usr/bin/pip

[root@python3 ~]# pip --version
pip 10.0.1 from /usr/local/sbin/python-3.7/lib/python3.7/site-packages/pip (python 3.7)複製代碼

大功告成。阿里雲

若是還有問題未能獲得解決,搜索887934385交流羣,進入後下載資料工具安裝包等。最後,感謝觀看!

相關文章
相關標籤/搜索