1、下载python源码
wget https://www.python.org/ftp/python/3.6.4/,
2、然后下载Python编译所必需的依赖的包,
yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y
3、解压python
tar -zxvf Python-3.6.4.tgz到当前目录下生成Python-3.6.4
4、cd 到Python-3.6.4下,
[root@oldboyedu Python-3.6.4]# ls
aclocal.m4 config.sub configure.ac Grammar install-sh LICENSE Makefile.pre.in Modules Parser PCbuild pyconfig.h.in README.rst Tools
config.guess configure Doc Include Lib Mac Misc Objects PC Programs Python setup.py
其中的configure是一个脚本文件,用来生成Makefile(后面编译python源码的)
[root@oldboyedu Python-3.6.4]# ./configure --prefix=/opt/python36
--prefix可以用来指定安装路径,此时Python3.6.4还是被解压的源码,并不会在/opt/下生成python36,
完成后可以看见Python3.6.4里多了刚才执行后出来的Makefile
接着[root@oldboyedu Python-3.6.4]# make进行编译
完成编译后进行安装[root@oldboyedu Python-3.6.4]# make install(到这一步python36才会在目录下)此时Python3.6.4完全安装好
5、配置环境变量
[root@oldboyedu opt]# echo $PATH 查看系统环境变量
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@oldboyedu ~]# ls /opt/python36/bin/ 查看到python3.6的可执行文件
vim /etc/profile
最后添加一行 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/python36/bin
[root@oldboyedu bin]# source /etc/profile 读一下这个文件,python3就在环境变量里了
然后在任意目录下python3就可以发现python3.6可运行了
通过pip3镜像安装第三方模块
[root@oldboyedu ~]# pip3 install django==1.11.20 -i https://pypi.tuna.tsinghua.edu.cn/simple (清华镜像)
注意,可指定版本,在pip3 install 后面加入包==版本号