1. 程式人生 > >【轉】新裝的CentOS 7安裝python3

【轉】新裝的CentOS 7安裝python3

url dev 來源 -m ces man root 目錄 安裝python

https://blog.csdn.net/lovefengruoqing/article/details/79284573

centos7 自帶有 python,但是卻是 python2 版本的 python,如果你想安裝個python3怎麽辦呢?難道要從github上把源碼clone下來進行編譯安裝麽?沒錯!因為 yum 源中並沒有現成的 python3 程序,所以必須要自己手動編譯安裝。

但是你也不用害怕,跟著我的步驟往下走,相信我,你也會在 centos7 上輕松的裝上 python3 的。

1.首先,你要知道系統現在的python的位置在哪兒:
[root@root ~]# whereis python
python: /usr/bin/python2.7 /usr/bin/python /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz
1
2
可以知道我們的python在 /usr/bin目錄中

[root@root ~]# cd /usr/bin/
[root@root bin]# ll python*
lrwxrwxrwx. 1 root root 7 2月 7 09:30 python -> python2
lrwxrwxrwx. 1 root root 9 2月 7 09:30 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 8月 4 2017 python2.7
1
2
3
4
5
可以看到,python指向的是python2,python2指向的是python2.7,因此我們可以裝個python3,然後將python指向python3,然後python2指向python2.7,那麽兩個版本的python就能共存了。

2.因為我們要安裝python3,所以要先安裝相關包,用於下載編譯python3:
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel

運行了以上命令以後,就安裝了編譯python3所用到的相關依賴

3.默認的,centos7也沒有安裝pip,不知道是不是因為我安裝軟件的時候選擇的是最小安裝的模式。
#運行這個命令添加epel擴展源
yum -y install epel-release

#安裝pip
yum install python-pip
1
2
3
4
5
4.用pip裝wget
pip install wget
1
5.用wget下載python3的源碼包
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
1
6.編譯python3源碼包
#解壓
xz -d Python-3.6.4.tar.xz
tar -xf Python-3.6.4.tar

#進入解壓後的目錄,依次執行下面命令進行手動編譯
./configure prefix=/usr/local/python3
make && make install
1
2
3
4
5
6
7
如果最後沒提示出錯,就代表正確安裝了,在/usr/local/目錄下就會有python3目錄

出錯了:

Python3: ImportError: No module named ‘_ctypes‘ when using Value from module multiprocessing

sudo yum -y install libffi-devel

./configure

Make&&make install

7.添加軟鏈接
#將原來的鏈接備份
mv /usr/bin/python /usr/bin/python.bak

#添加python3的軟鏈接
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python

#測試是否安裝成功了
python -V
1
2
3
4
5
6
7
8
8.更改yum配置,因為其要用到python2才能執行,否則會導致yum不能正常使用
vi /usr/bin/yum
把#! /usr/bin/python修改為#! /usr/bin/python2

vi /usr/libexec/urlgrabber-ext-down
把#! /usr/bin/python 修改為#! /usr/bin/python2
---------------------
作者:純愛楓若情
來源:CSDN
原文:https://blog.csdn.net/lovefengruoqing/article/details/79284573?utm_source=copy
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

【轉】新裝的CentOS 7安裝python3