1. 程式人生 > >CentOS7 安裝 Python3.6

CentOS7 安裝 Python3.6

with pre ack mic zlib evel 模塊 building cache

先看編譯安裝,編譯安裝後的文件目錄基本和Windows平臺上目錄差不多。指定安裝路徑的話,所有的文件都是在一起的。
後面有yum的安裝的方法。

編譯安裝

編譯安裝,會裝好setuptools和pip這兩個工具,都在site-packages這個文件夾裏。之後再用pip安裝各種模塊,也都在pip所在的site-packages文件夾裏。總之,所有的東西都在python3的安裝目錄下。

下載安裝包

[root@Python36 ~]# cd ~
[root@Python36 ~]# mkdir download
[root@Python36 ~]# cd download/
[root@Python36 download]# wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
--2018-09-29 15:49:52--  https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
正在解析主機 www.python.org (www.python.org)... 151.101.108.223, 2a04:4e42:36::223
正在連接 www.python.org (www.python.org)|151.101.108.223|:443... 已連接。
已發出 HTTP 請求,正在等待回應... 200 OK
長度:22930752 (22M) [application/octet-stream]
正在保存至: “Python-3.6.6.tgz”

100%[==================================================================>] 22,930,752  59.5KB/s 用時 7m 48s 

2018-09-29 15:57:40 (47.9 KB/s) - 已保存 “Python-3.6.6.tgz” [22930752/22930752])

[root@Python36 download]#

下載後解壓:

[root@Python36 download]# tar -zxvf Python-3.6.6.tgz

安裝依賴包

我一開始並沒有裝下面的依賴包,而是直接編譯安裝,等報錯了再找解決辦法:

[root@Python36 ~]# yum install gcc zlib-devel

.install 的時候需要gcc
make install 的時候需要zlib-devel

編譯安裝

如果缺少依賴包,編輯的時候就會有問題。到時候就查看錯誤信息,到網上搜一下,看看是缺少了哪個依賴包:

[root@Python36 download]# cd Python-3.6.6
[root@Python36 Python-3.6.6]# ./configure prefix=/usr/local/python3

指定安裝目錄是:/usr/local/python3。
如果不指定安裝目錄的話,安裝後可執行文件默認放在/usr/local/bin(默認放這裏的可執行文件後面就不用另外再創建軟鏈接了),庫文件默認放在/usr/local/lib,配置文件默認放在/usr/local/etc,其它的資源文件放在/usr/local/share,比較淩亂。似乎也不是太亂的樣子,不過網上的文章都推薦在默認位置下再建一級目錄,全部放一起,就是這裏的做法。
我們指定安裝目錄,以後卸載的話直接刪除目錄就可以幹凈卸載了。
然後make:

[root@Python36 Python-3.6.6]# make

這裏遇到報錯了:

gcc -pthread   -Xlinker -export-dynamic -o Programs/_testembed Programs/_testembed.o libpython3.6m.a -lpthread -ldl  -lutil   -lm  
# Substitution happens here, as the completely-expanded BINDIR
# is not available in configure
sed -e "s,@EXENAME@,/usr/local/python3/bin/python3.6m," < ./Misc/python-config.in >python-config.py
# Replace makefile compat. variable references with shell script compat. ones;  -> 
LC_ALL=C sed -e ‘s,\$(\([A-Za-z0-9_]*\)),\$\{\1\},g‘ < Misc/python-config.sh >python-config
# On Darwin, always use the python version of the script, the shell
# version doesn‘t use the compiler customizations that are provided
# in python (_osx_support.py).
if test `uname -s` = Darwin; then         cp python-config.py python-config; fi
[root@Python36 Python-3.6.6]#

直接根據提示運行上面給的命令,然後再make:

[root@Python36 Python-3.6.6]# sed -e "s,@EXENAME@,/usr/local/python3/bin/python3.6m," < ./Misc/python-config.in >python-config.py
[root@Python36 Python-3.6.6]# make
 CC=‘gcc -pthread‘ LDSHARED=‘gcc -pthread -shared  ‘ OPT=‘-DNDEBUG -g -fwrapv -O3 -Wall‘        _TCLTK_INCLUDES=‘‘ _TCLTK_LIBS=‘‘   ./python -E ./setup.py  build
running build
running build_ext
INFO: Can‘t locate Tcl/Tk libs and/or headers
warning: building with the bundled copy of libffi is deprecated on this platform.  It will not be distributed with Python 3.7

Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2                  _curses               _curses_panel      
_dbm                  _gdbm                 _lzma              
_sqlite3              _ssl                  _tkinter           
readline              zlib                                     
To find the necessary bits, look in setup.py in detect_modules() for the module‘s name.

The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
atexit                pwd                   time               
running build_scripts
copying and adjusting /root/download/Python-3.6.6/Tools/scripts/pydoc3 -> build/scripts-3.6
copying and adjusting /root/download/Python-3.6.6/Tools/scripts/idle3 -> build/scripts-3.6
copying and adjusting /root/download/Python-3.6.6/Tools/scripts/2to3 -> build/scripts-3.6
copying and adjusting /root/download/Python-3.6.6/Tools/scripts/pyvenv -> build/scripts-3.6
changing mode of build/scripts-3.6/pydoc3 from 644 to 755
changing mode of build/scripts-3.6/idle3 from 644 to 755
changing mode of build/scripts-3.6/2to3 from 644 to 755
changing mode of build/scripts-3.6/pyvenv from 644 to 755
renaming build/scripts-3.6/pydoc3 to build/scripts-3.6/pydoc3.6
renaming build/scripts-3.6/idle3 to build/scripts-3.6/idle3.6
renaming build/scripts-3.6/2to3 to build/scripts-3.6/2to3-3.6
renaming build/scripts-3.6/pyvenv to build/scripts-3.6/pyvenv-3.6
[root@Python36 Python-3.6.6]# 

make 成功後就是 make install :

[root@Python36 Python-3.6.6]# make install

驗證

直接先運行python3,再確認一下版本信息:

[root@Python36 ~]# cd /usr/local/python3/
[root@Python36 python3]# ./bin/python3
Python 3.6.6 (default, Sep 29 2018, 16:19:00) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@Python36 python3]# 

退出用 exit()

創建軟鏈接

直接執行python3命令是找不到的,在python3的啟動命令加一條軟鏈接到系統目錄:

[root@Python36 ~]# python3
-bash: python3: 未找到命令
[root@Python36 ~]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@Python36 ~]# python3
Python 3.6.6 (default, Sep 29 2018, 16:19:00) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

pip工具是安裝各種python包的,也很常用,也加個軟鏈接:

[root@Python36 ~]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
[root@Python36 ~]# pip3 --version
pip 10.0.1 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)

用python命令進入python3

還可以把默認的python的軟鏈接也指向python3,就是把系統默認的用python命令啟動python2給替換成啟動python3。這樣做的問題是,系統的yum使用要python2,因此替換為python3後,yum就無法正常工作了。
解決辦法就是修改yum的配置文件 vi /usr/bin/yum ,然後找到開頭的 #!/usr/bin/python 修改為 #!/usr/bin/python2.7 保存退出就好了。

yum安裝

用yum的話,步驟簡單多了,就是安裝路徑會分散在不同的目錄裏。

安裝epel源

用下面的命令就可以安裝好epel源。不過國外的源可能會比較慢,可以都換成國內的yum源。

[root@Python3 ~]# yum install epel-release

上面的步驟就不要做了。
先確認有沒有wget命令:

[root@Python3 ~]# wget --version

如果沒有就先裝一下,之後下載yum源的文件要用:

[root@Python3 ~]# yum install wget

備份現在的yum源
備份是個好習慣,直接刪也可以。然後發現wget沒裝,現在一個yum源也沒有了,只能找其他辦法了。

[root@Python3 ~]# cd /etc/yum.repos.d/
[root@Python3 yum.repos.d]# mkdir bak
[root@Python3 yum.repos.d]# mv *.repo bak
[root@Python3 yum.repos.d]# ls
bak

現在文件夾已經空了,去下載yum源的repo文件,我用的是阿裏的源:

[root@Python3 yum.repos.d]# wget https://mirrors.aliyun.com/repo/Centos-7.repo
[root@Python3 yum.repos.d]# wget https://mirrors.aliyun.com/repo/epel-7.repo

然後可以makecache建立本地緩存:

[root@Python3 yum.repos.d]# yum makecache

安裝python

這裏裝了python,另外還裝了一個setuptools,之後的pip工具要通過setuptools來安裝。

[root@Python3 yum.repos.d]# yum install python36 python36-setuptools

安裝完之後要用python36命令來啟動,這裏可以添加一下python3命令的軟連接:

[root@Python3 ~]# ln -s python36 /usr/bin/python3

安裝pip

通過setuptools的easyinstall命令來安裝pip:

[root@Python3 ~]# easy_install-3.6 pip

安裝目錄

python 和 setuptools 都在這個目錄裏:
/usr/lib/python3.6/site-packages/
而 pip 是在下面這個目錄裏,就是默認編譯安裝的目錄:
/usr/local/lib/python3.6/site-packages
之後用pip安裝的模塊也都會在pip的這個目錄裏。

CentOS7 安裝 Python3.6