python3 编译安装

时间:2021-12-18 17:28:45

前言:

  Linux下大部分系统默认自带python2.x的版本,最常见的是python2.6或python2.7版本,默认的python被系统很多程序所依赖,比如centos下的yum就是python2写的,所以默认版本不要轻易删除,否则会有一些问题,如果需要使用最新的Python3那么我们可以编译安装源码包到独立目录,这和系统默认环境之间是没有任何影响的,python3和python2两个环境并存即可

  预处理

[root@python ~]# python -V        //查看自带版本
Python 2.7.
[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./
[root@python Python-3.7.]# pwd
/root/python3/Python-3.7.
[root@python Python-3.7.]# 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.]# sed -ri 's/^#readline/readline/' Modules/Setup.dist
[root@python Python-3.7.]# sed -ri 's/^#(SSL=)/\1/' Modules/Setup.dist
[root@python Python-3.7.]# sed -ri 's/^#(_ssl)/\1/' Modules/Setup.dist
[root@python Python-3.7.]# sed -ri 's/^#([\t]*-DUSE)/\1/' Modules/Setup.dist
[root@python Python-3.7.]# sed -ri 's/^#([\t]*-L\$\(SSL\))/\1/' Modules/Setup.dist

  设置完成,进行编译前的配置

[root@python Python-3.7.]# ./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.]# make -j 

[root@python Python-3.7.]# make install
//执行最后有以下
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-18.1 setuptools-40.6.

  编译安装完成

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

pip 安装

[root@python Python-3.7.]# cd ..
[root@python python3]# ls
Python-3.7. Python-3.7..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. from /usr/python/lib/python3./site-packages/pip (python 3.7)
[root@python ~]#