1. 程式人生 > >Python的詳細安裝步驟二—— Linux 環境

Python的詳細安裝步驟二—— Linux 環境

二、Linux 升級 Python
Linux 作業系統本身自帶安裝 Python,不過一般都是 2.x 版本的,2.x 和 3.x 是互不相容的。
以下是在 CentOS 中安裝 Python 3.6.5 的過程。
1.下載
1)進入官網,選擇 Downloads,然後選擇 Source code
在這裡插入圖片描述
2)選擇需要的對應版本,我選擇的 3.6.5 對應 Gzipped source tarball
在這裡插入圖片描述
3)解壓檔案,解壓之後生成目錄 Python-3.6.5

[[email protected] ~]$ tar -xzvf Python-3.6.5.tgz 
[[email protected]
~]$ ll 總用量 22464 drwxrwxr-x. 2 Vicky Vicky 96 10月 19 10:38 code -rw-r--r--. 1 Vicky Vicky 275 6月 14 15:59 mysql drwxrwxr-x. 6 Vicky Vicky 56 4月 27 15:48 net drwxrwxr-x. 3 Vicky Vicky 53 4月 27 15:46 netcdf drwxrwxr-x. 2 Vicky Vicky 6 12月 1 2017 perl5 drwxr-xr-x. 16 Vicky Vicky 4096 3月 28 2018 Python-3.6.5 -rw-rw-r--. 1 Vicky Vicky 22994617 10月 19 10:20 Python-3.6.5.tgz

4)建立新的 python 的安裝目錄。
進入 /usr/local 建立目錄 python3,用此目錄作為 python3 的安裝路徑,避免覆蓋老的版本,使得引用 python 的程式出現錯誤。

[[email protected] ~]$ cd /usr/local
[[email protected] local]$ mkdir python3
mkdir: 無法建立目錄"python3": 許可權不夠   
[[email protected] local]$ su
密碼:
[[email protected] local]# mkdir python3
[
[email protected]
local]# ll 總用量 0 drwxr-xr-x. 2 root root 6 11月 5 2016 bin drwxr-xr-x. 2 root root 6 11月 5 2016 etc drwxr-xr-x. 2 root root 6 11月 5 2016 games drwxr-xr-x. 2 root root 35 9月 3 17:50 include drwxr-xr-x. 3 root root 91 9月 3 17:50 lib drwxr-xr-x. 2 root root 6 11月 5 2016 lib64 drwxr-xr-x. 2 root root 6 11月 5 2016 libexec drwxr-xr-x. 2 root root 6 10月 19 10:39 python3 drwxr-xr-x. 2 root root 6 11月 5 2016 sbin drwxr-xr-x. 5 root root 49 12月 1 2017 share drwxr-xr-x. 2 root root 6 11月 5 2016 src drwxr-xr-x. 5 root root 45 10月 15 16:06 zlib

5)使用 root 身份編譯,安裝 python3

[[email protected] ~]# cd /home/Vicky/Python-3.6.5/
[[email protected] Python-3.6.5]# ./configure  --prefix=/usr/local/python3
[[email protected] bin]# make
[[email protected] bin]# make install

安裝成功之後,在 /usr/lib 下會生成 bin, include,lib,share 四個目錄。

[[email protected] Python-3.6.5]# cd /usr/local/python3/
[[email protected] python3]# ls
bin  include  lib  share

6)建立版本連線
1)原有連線

[[email protected] bin]# ls -la python*
lrwxrwxrwx. 1 root root    7 12月  1 2017 python -> python2
lrwxrwxrwx. 1 root root    9 12月  1 2017 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 11月  6 2016 python2.7
-rwxr-xr-x. 1 root root 1835 11月  6 2016 python2.7-config
lrwxrwxrwx. 1 root root   16 12月  1 2017 python2-config -> python2.7-config
lrwxrwxrwx. 1 root root   14 12月  1 2017 python-config -> python2-config

2)建立 python3 的連線

[[email protected] bin]# ln -s /usr/local/python3/bin/python3 python3
[[email protected] bin]# ls -la python*
lrwxrwxrwx. 1 root root    7 12月  1 2017 python -> python2
lrwxrwxrwx. 1 root root    9 12月  1 2017 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 11月  6 2016 python2.7
-rwxr-xr-x. 1 root root 1835 11月  6 2016 python2.7-config
lrwxrwxrwx. 1 root root   16 12月  1 2017 python2-config -> python2.7-config
lrwxrwxrwx. 1 root root   30 10月 19 10:54 python3 -> /usr/local/python3/bin/python3
lrwxrwxrwx. 1 root root   14 12月  1 2017 python-config -> python2-config

7)檢查是否安裝成功
a.檢查原有 python 命令是否可用

[[email protected] bin]# python
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

b.驗證新安裝的 python3 是否可用,輸入 python3 可進入互動式操作環境,並顯示 Python 3.6.5 的版本資訊,則安裝成功。

[[email protected] bin]# python3
Python 3.6.5 (default, Oct 19 2018, 10:46:59) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

至此,python3 安裝成功,這種安裝不會覆蓋原有的 python。

宣告:
本部落格的所有內容,僅是自己的一些學習筆記,如有錯誤,歡迎指正。如有侵權,請告知修改。