python3 編譯安裝

前言:

  Linux下大部分系統默認自帶python2.x的版本,最多見的是python2.6或python2.7版本,默認的python被系統不少程序所依賴,好比centos下的yum就是python2寫的,因此默認版本不要輕易刪除,不然會有一些問題,若是須要使用最新的Python3那麼咱們能夠編譯安裝源碼包到獨立目錄,這和系統默認環境之間是沒有任何影響的,python3和python2兩個環境並存便可python

  預處理

[root@python ~]# python -V        //查看自帶版本
Python 2.7.5 [root@python ~]# mkdir python3 [root@python ~]# cd python3/ [root@python ~]# wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz //目前最新版本 

  準備好python的源碼包,接下來準備編譯安裝所需的依賴包;

[root@python ~]# yum -y install zlib-devel bzip2-devel openssl-devel sqlite-devel readline-devel [root@python ~]# yum install libffi-devel -y

   編譯前預設

[root@python ~]# cd python3/Python-3.7.2/ [root@python Python-3.7.2]# pwd
/root/python3/Python-3.7.2 [root@python Python-3.7.2]# ls aclocal.m4 Doc m4 Parser README.rst CODE_OF_CONDUCT.rst Grammar Mac PC setup.py config.guess Include Makefile.pre.in PCbuild Tools config.sub install-sh Misc Programs configure Lib Modules pyconfig.h.in configure.ac LICENSE Objects Python [root@python Python-3.7.2]# sed -ri 's/^#readline/readline/' Modules/Setup.dist [root@python Python-3.7.2]# sed -ri 's/^#(SSL=)/\1/' Modules/Setup.dist [root@python Python-3.7.2]# sed -ri 's/^#(_ssl)/\1/' Modules/Setup.dist [root@python Python-3.7.2]# sed -ri 's/^#([\t]*-DUSE)/\1/' Modules/Setup.dist [root@python Python-3.7.2]# sed -ri 's/^#([\t]*-L\$\(SSL\))/\1/' Modules/Setup.dist

  設置完成,進行編譯前的配置

[root@python Python-3.7.2]# ./configure  --prefix=/usr/python //看到如下結果
configure: creating ./config.status config.status: creating Makefile.pre config.status: creating Misc/python.pc config.status: creating Misc/python-config.sh config.status: creating Modules/ld_so_aix config.status: creating pyconfig.h creating Modules/Setup creating Modules/Setup.local creating Makefile //這個能夠忽略,不會影響後面的安裝
If you want a release build with all stable optimizations active (PGO, etc), please run ./configure --enable-optimizations

  開始編譯

[root@python Python-3.7.2]# make -j 4 [root@python Python-3.7.2]# make install
//執行最後有如下
Collecting setuptools Collecting pip Installing collected packages: setuptools, pip Successfully installed pip-18.1 setuptools-40.6.2

  編譯安裝完成

[root@python Python-3.7.2]# ln -s /usr//python/bin/python3 /usr/bin/python3
[root@python Python-3.7.2]# which python3 /bin/python3 [root@python Python-3.7.2]# python3 -V Python 3.7.2

pip 安裝

[root@python Python-3.7.2]# cd .. [root@python python3]# ls Python-3.7.2  Python-3.7.2.tgz [root@python python3]# curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

python3 綁定pip

[root@python python3]# python3 get-pip.py        //使用哪一個python版本,pip就會綁定在哪一個版本

完成安裝

[root@python ~]# ln -s /usr/python/bin/pip3 /usr/bin/pip3 [root@python ~]# which pip3 /bin/pip3 [root@python ~]# pip3 -V pip 19.0.3 from /usr/python/lib/python3.7/site-packages/pip (python 3.7) [root@python ~]#
相關文章
相關標籤/搜索