Python開發【初始篇】:Linux下安裝Python3


Linux系統默認自帶python2.6的版本,這個版本被系統很多程序所依賴,所以建議不要輕易刪除,除非你能解決其他程序的依賴問題。如果使用最新的Python3需要進行編譯安裝源碼包,這樣就對系統默認的包沒有任何影響。

  1. [root@test-c2c-console01 ~]# cat /etc/redhat-release
  2. CentOS release 6.6 (Final)
  3. [root@test-c2c-console01 ~]# uname -a
  4. 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
  5. [root@test-c2c-console01 ~]# python -V
  6. Python 2.6.6

Python官網下載pthon3源碼包,https://www.python.org/

選擇自己需要的版本

選擇源碼包

下載到本地然后上傳到linux或者復制下載鏈接直接通過wget下載

  1. [root@test-c2c-console01 tools]# wget -q https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
  2. [root@test-c2c-console01 tools]# ll Python-3.5.2.tgz
  3. -rw-r--r-- 1 root root 20566643 Jun 26 2016 Python-3.5.2.tgz

解壓並安裝

  1. [root@test-c2c-console01 tools]# tar xf Python-3.5.2.tgz
  2. [root@test-c2c-console01 tools]# cd Python-3.5.2
  3. [root@test-c2c-console01 Python-3.5.2]# ./configure --prefix=/application/Python-3.5.2/ #指定安裝目錄
  4. [root@test-c2c-console01 Python-3.5.2]# make && make install

創建軟連接

  1. [root@test-c2c-console01 Python-3.5.2]# cd /application/
  2. [root@test-c2c-console01 application]# ln -s Python-3.5.2/ Python3
  3. [root@test-c2c-console01 application]# ll
  4. total 4
  5. lrwxrwxrwx 1 root root 13 Mar 1 09:08 Python3 -> Python-3.5.2/
  6. drwxr-xr-x 6 root root 4096 Mar 1 09:03 Python-3.5.2

到此python3就安裝好了,啟動pyhon3。

  1. [root@test-c2c-console01 application]# /application/Python3/bin/python3
  2. Python 3.5.2 (default, Mar 1 2017, 09:02:01)
  3. [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>>

由於這樣啟動比較麻煩可以通過配置環境變量解決。

  1. [root@test-c2c-console01 application]# vim /etc/profile # 文件末尾添加python3路徑
  2. PATH="/application/Python3/bin/:$PATH"
  3. [root@test-c2c-console01 application]# source /etc/profile # 更新環境變量
  4. [root@test-c2c-console01 application]# which python3
  5. /application/Python3/bin/python3
  6. [root@test-c2c-console01 application]# which python
  7. /usr/bin/python
  8. [root@test-c2c-console01 application]# python3 -V
  9. Python 3.5.2
  10. [root@test-c2c-console01 application]# python -V
  11. Python 2.6.6

啟動系統默認的python

  1. [root@test-c2c-console01 application]# python
  2. Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
  3. [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>>

啟動python3

  1. [root@test-c2c-console01 application]# python3
  2. Python 3.5.2 (default, Mar 1 2017, 09:02:01)
  3. [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>>

 


免責聲明!

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



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