python3.6.6安装
1.下载python3源码包
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
2.下载python3编译的依赖包
yum install -y 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
3.解压缩源码包
tar -zxvf Python-3.6.6.tgz
4.进入源码包文件夹
cd Python-3.6.6
5.编译且安装
./configure --prefix=/opt/python36 # 指定安装目录为/opt/python36
make && make install # 先进行make,执行完成之后,才开始执行make install
进入/opt目录查看python36文件夹,我们要的python3都在这里了
/opt/python36/bin/python3 /opt/python36/bin/pip3
6.更改linux的path变量,添加python3
修改/etc/profile系统环境变量配置文件,添加以下内容
vim /etc/profile
#添加
PATH=/opt/python36/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
7. 为了永久生效path设置,添加到/etc/profile全局环境变量配置文件中 重载配置文件/etc/profile
source /etc/profile
8 .进入pyhon3
[root@localhost opt]# python3
Python 3.6.6 (default, Aug 23 2021, 12:19:33)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
python3.7.2安装
1.准备环境
yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y
yum install libXcomposite libXcursor libXi libXtst libXrandr alsa-lib mesa-libEGL libXdamage mesa-libGL libXScrnSaver -y
yum install libffi-devel -y
# Python3.7及以上版本需要安装libffi-devel,否则安装时会报错 ModuleNotFoundError: No module named 'ctypes'错误
2.下载Python安装包
wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
3.解压
xz -d Python-3.7.2.tar.xz
tar -xvf Python-3.7.2.tar
4.编译与安装
cd Python-3.7.2/
./configure --enable-shared --prefix=/usr/local CFLAGS=-fPIC LDFLAGS="-Wl,-rpath /usr/local/lib"
make -j24
make install
5.做软链接
ln -sf /usr/local/bin/python3.7 /usr/bin/python3
ln -sf /usr/local/bin/python3.7 /usr/bin/python3.7
ln -sf /usr/local/bin/pip3.7 /usr/bin/pip3
ln -sf /usr/local/bin/pip3.7 /usr/bin/pip3.7
6.测试安装结果:
[root@localhost Python-3.7.2]# python3 --version
Python 3.7.2