Linux安裝Python3

欲語與雨發表於2018-08-28
1、檢視Python版本
[root@93 ~]# python -V  #注意,是大寫的V
Python 2.7.5

2、安裝Python可能需要依賴的庫

 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel 

3、下載Python3的壓縮包

到Python官網下載 https://www.python.org

wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0b5.tgz

4、解壓縮Python壓縮包

[root@Zabbix93 ~]# tar -zxf Python-3.7.0b5.tgz 
[root@Zabbix93 ~]# cd Python-3.7.0b5
[root@Zabbix93 Python-3.7.0b5]# ls
aclocal.m4    configure     Grammar     Lib      Mac              Modules  PC        pyconfig.h.in  setup.py
config.guess  configure.ac  Include     LICENSE  Makefile.pre.in  Objects  PCbuild   Python         Tools
config.sub    Doc           install-sh  m4       Misc             Parser   Programs  README.rst

5、編譯安裝Python3.7

./configure --prefix=/usr/local/ //安裝到/usr/local目錄

我這裡提示報錯,原因:缺少編譯器。解決方法:安裝gcc

[root@Zabbix93 Python-3.7.0b5]# ./configure --prefix=/usr/local/
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.7... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/root/Python-3.7.0b5`:
configure: error: no acceptable C compiler found in $PATH
See `config.log` for more details



[root@Zabbix93 Python-3.7.0b5]# yum install -y gcc
[root@Zabbix93 Python-3.7.0b5]# ./configure --prefix=/usr/local/
[root@Zabbix93 Python-3.7.0b5]# make
[root@Zabbix93 Python-3.7.0b5]# make altinstall //此處不能用install安裝,因為install不區分版本,會出現多版本混亂的問題

6、更改Python預設連結

 python3.7程式的執行檔案:/usr/local/bin/python3.7
  python3.7應用程式目錄:/usr/local/lib/python3.7
  pip7的執行檔案:/usr/local/bin/pip3.7
  pyenv3的執行檔案:/usr/local/bin/pyenv-3.7

更改Python預設連結:

# cd/usr/bin
# mv  python python.backup
# ln -s /usr/local/bin/python3.7 /usr/bin/python
# ln -s /usr/local/bin/python3.7 /usr/bin/python3


相關文章