Linux系統默認自帶python2.6的版本,這個版本被系統很多程序所依賴,所以建議不要輕易刪除,除非你能解決其他程序的依賴問題。如果使用最新的Python3需要進行編譯安裝源碼包,這樣就對系統默認的包沒有任何影響。
-
[root@test-c2c-console01 ~]# cat /etc/redhat-release
-
CentOS release 6.6 (Final)
-
[root@test-c2c-console01 ~]# uname -a
-
Linux test-c2c-console01.bj 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
-
[root@test-c2c-console01 ~]# python -V
-
Python 2.6.6
Python官網下載pthon3源碼包,https://www.python.org/
選擇自己需要的版本
選擇源碼包
下載到本地然后上傳到linux或者復制下載鏈接直接通過wget下載
-
[root@test-c2c-console01 tools]# wget -q https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
-
[root@test-c2c-console01 tools]# ll Python-3.5.2.tgz
-
-rw-r--r-- 1 root root 20566643 Jun 26 2016 Python-3.5.2.tgz
解壓並安裝
-
[root@test-c2c-console01 tools]# tar xf Python-3.5.2.tgz
-
[root@test-c2c-console01 tools]# cd Python-3.5.2
-
[root@test-c2c-console01 Python-3.5.2]# ./configure --prefix=/application/Python-3.5.2/ #指定安裝目錄
-
[root@test-c2c-console01 Python-3.5.2]# make && make install
創建軟連接
-
[root@test-c2c-console01 Python-3.5.2]# cd /application/
-
[root@test-c2c-console01 application]# ln -s Python-3.5.2/ Python3
-
[root@test-c2c-console01 application]# ll
-
total 4
-
lrwxrwxrwx 1 root root 13 Mar 1 09:08 Python3 -> Python-3.5.2/
-
drwxr-xr-x 6 root root 4096 Mar 1 09:03 Python-3.5.2
到此python3就安裝好了,啟動pyhon3。
-
[root@test-c2c-console01 application]# /application/Python3/bin/python3
-
Python 3.5.2 (default, Mar 1 2017, 09:02:01)
-
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
-
Type "help", "copyright", "credits" or "license" for more information.
-
>>>
由於這樣啟動比較麻煩可以通過配置環境變量解決。
-
[root@test-c2c-console01 application]# vim /etc/profile # 文件末尾添加python3路徑
-
PATH="/application/Python3/bin/:$PATH"
-
[root@test-c2c-console01 application]# source /etc/profile # 更新環境變量
-
[root@test-c2c-console01 application]# which python3
-
/application/Python3/bin/python3
-
[root@test-c2c-console01 application]# which python
-
/usr/bin/python
-
[root@test-c2c-console01 application]# python3 -V
-
Python 3.5.2
-
[root@test-c2c-console01 application]# python -V
-
Python 2.6.6
啟動系統默認的python
-
[root@test-c2c-console01 application]# python
-
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
-
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
-
Type "help", "copyright", "credits" or "license" for more information.
-
>>>
啟動python3
-
[root@test-c2c-console01 application]# python3
-
Python 3.5.2 (default, Mar 1 2017, 09:02:01)
-
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
-
Type "help", "copyright", "credits" or "license" for more information.
-
>>>