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 //目前最新版本
[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
[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
[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 ~]#