在阿里雲CentOS服務器上安裝Python3.7並設置為默認Python


1.引言

Linux操作系統自帶一個python2.7,沒有python3,在開發的時候非常不便,因此需要安裝一個python3,並且將python3設置系統默認python,同時還不能影響那些Linux系統中需要用python2的底層文件。

2. 方法

2.1 查看操作系統及Python基本信息

cat /etc/redhat-release    # 查看內核版本
python -V                  # 查看python版本
which python               # 查看python路徑

我這邊的系統的內核為CentOS 7,默認python的版本為2.7.5,路徑為/usr/bin/python。

然后我們導航到該目錄,查看python相關文件的信息,可以看到python和python2指向的都是python2.7。

[root@libra-server ~]# cd /usr/bin
 
[root@libra-server bin]# ll python*    # 查看以python開頭的文件信息
lrwxrwxrwx. 1 root root    7 Oct 15  2017 python -> python2
lrwxrwxrwx. 1 root root    9 Oct 15  2017 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Aug  4  2017 python2.7

2.2 安裝依賴包(編譯,安裝程序等所需)

yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel 
yum -y install readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
yum -y install libffi-devel

(如果是RedHat/CentOS平台,用yum install;如果是Ubuntu/Debian平台,用apt-get install)

2.3 下載Python

到Python官方頁面選擇一個版本的Python:Python Source Releases,選擇“XZ compressed source tarball

然后用wget命令下載到用戶目錄

[root@libra-server bin]# cd ~
[root@libra-server ~]# wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz

下載完畢后可以看到Python源碼已經靜靜的躺在你的目錄里了,

[root@libra-server ~]# ls
Python-3.7.4.tar.xz  QQ9.0.9_1.exe  wget-log

2.4 編譯並安裝Python

首先對剛剛下載的壓縮包進行解壓,

[root@libra-server ~]# tar -xvJf Python-3.7.4.tar.xz

進入解壓后的目錄,可以看到Python的源碼文件,其中configure用來配置,配置完成后會生成用來安裝的Makefile,

[root@libra-server ~]# cd Python-3.7.4
[root@libra-server Python-3.7.4]# ll
total 1060
-rw-r--r--  1 501 501  10953 Jul  9 02:03 aclocal.m4
-rw-r--r--  1 501 501    631 Jul  9 02:03 CODE_OF_CONDUCT.rst
-rwxr-xr-x  1 501 501  44166 Jul  9 02:03 config.guess
-rwxr-xr-x  1 501 501  36251 Jul  9 02:03 config.sub
-rwxr-xr-x  1 501 501 503641 Jul  9 02:03 configure
-rw-r--r--  1 501 501 167840 Jul  9 02:03 configure.ac
drwxr-xr-x 18 501 501   4096 Jul  9 02:31 Doc
drwxr-xr-x  2 501 501   4096 Jul  9 02:03 Grammar
drwxr-xr-x  3 501 501   4096 Jul  9 02:03 Include
-rwxr-xr-x  1 501 501   7122 Jul  9 02:03 install-sh
drwxr-xr-x 33 501 501   4096 Jul  9 02:03 Lib
-rw-r--r--  1 501 501  12769 Jul  9 02:03 LICENSE
drwxr-xr-x  2 501 501   4096 Jul  9 02:03 m4
drwxr-xr-x  8 501 501   4096 Jul  9 02:03 Mac
-rw-r--r--  1 501 501  63658 Jul  9 02:03 Makefile.pre.in
drwxr-xr-x  2 501 501   4096 Jul  9 02:31 Misc
drwxr-xr-x 13 501 501   4096 Jul  9 02:03 Modules
drwxr-xr-x  4 501 501   4096 Jul  9 02:03 Objects
drwxr-xr-x  2 501 501   4096 Jul  9 02:03 Parser
drwxr-xr-x  6 501 501   4096 Jul  9 02:03 PC
drwxr-xr-x  2 501 501   4096 Jul  9 02:03 PCbuild
drwxr-xr-x  2 501 501   4096 Jul  9 02:03 Programs
-rw-r--r--  1 501 501  43204 Jul  9 02:03 pyconfig.h.in
drwxr-xr-x  3 501 501   4096 Jul  9 02:03 Python
-rw-r--r--  1 501 501  10113 Jul  9 02:03 README.rst
-rw-r--r--  1 501 501 103776 Jul  9 02:03 setup.py
drwxr-xr-x 23 501 501   4096 Jul  9 02:03 Tools

配置安裝目錄,

[root@libra-server Python-3.7.4]# ./configure prefix=/usr/local/python3 --enable-optimizations

注:有時候如果加上開啟優化選項 "--enable-optimizations",下面編譯 make 時會報錯“Fatal Python error: _PySys_BeginInit: can't initialize sys module”,可能和內核版本有關系,如果報錯可以去掉該選項然后重新配置並編譯。

編譯並安裝Python

[root@libra-server Python-3.7.4]# make
[root@libra-server Python-3.7.4]# make install

然后cd到 /usr/local,可以看到python3已經安裝好了

[root@libra-server bin]# cd /usr/local/
[root@libra-server local]# ls
aegis  bin  etc  games  include  lib  lib64  libexec  python3  sbin  share  src

2.5 將python3添加到系統命令

將 /python3/bin中的python3 軟鏈到 /usr/bin/python,使以后執行 python 命令時都會指向 python3

[root@libra-server ~]# ln -s /usr/local/python3/bin/python3 /usr/bin/python

然后執行python,可以發現已經是3.7.4版本的了

[root@libra-server ~]# python
Python 3.7.4 (default, Aug 21 2019, 15:07:15) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

2.6 將需要python2的程序重定向到python2

由於yum需要python2,所以需要把yum文件重新指向python2

[root@libra-server ~]# vi /usr/bin/yum

然后將第一行的#!/usr/bin/python更改為#!/usr/bin/python2,yum就可以執行了。

同理,/usr/libexec/urlgrabber-ext-down 這個文件也做一下相同的操作

2.7 配置pip3

如下所示可以看到,pip3還沒有添加進系統目錄里,因此同樣需要做一下軟鏈。

[root@libra-server ~]# pip2 -V
pip 9.0.1 from /usr/lib/python2.7/site-packages (python 2.7)
[root@libra-server ~]# pip -V
pip 9.0.1 from /usr/lib/python2.7/site-packages (python 2.7)
[root@libra-server ~]# pip3 -V
-bash: pip3: command not found

將原來的pip備份,然后把pip3軟鏈到pip,這樣以后執行pip就是pip3,pip2就是pip2了:

[root@libra-server ~]# mv /usr/bin/pip /usr/bin/pip.bak
[root@libra-server ~]# ln -s /usr/local/python3/bin/pip3  /usr/bin/pip
[root@libra-server ~]# pip -V
pip 19.0.3 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)

3. 結尾

基本上就是這樣了,步驟基本上可以分為:

① 安裝依賴庫並備份原/usr/bin目錄下的python

② 下載並編譯安裝python3

③ 建立python3和pip3的軟鏈

④ 把需要python2的系統文件改寫,重新指向python2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM