linux centos安装python3.8 作者:马育民 • 2022-04-28 23:17 • 阅读:10463 # 说明 centos7 自带有 python,版本是 `python2.7` linux 安装 `python3`,需要先下载源码,然后编译 # 查看已安装python的位置 ``` whereis python ``` 执行结果如下: ``` python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz ``` 可以看到已经安装 `python2.7` 、安装的目录位置 # 下载 python 源码 ### 创建文件夹 ``` mkdir /program ``` ``` cd /program ``` ### 下载 网页:https://www.python.org/downloads/source/ 执行下面命令下载 Python-3.8.13 的源码 ``` wget https://www.python.org/ftp/python/3.8.13/Python-3.8.13.tgz ``` 解压缩: ``` tar -zxvf Python-3.8.12.tgz --no-same-owner ``` # 安装编译环境 ``` yum update ``` ``` yum -y install gcc ``` ``` yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel ``` # 配置 ``` cd Python-3.8.12 ``` ### 方式一:默认安装目录 ``` ./configure ``` 默认的安装目录是: - `/usr/local/bin/python3` - `/usr/local/bin/pip3` 注意:不要加 `--enable-optimizations` 参数 ### 方式二:指定安装目录 安装到 `/usr/local/python3` 目录下: ``` ./configure --prefix=/usr/local/python3 ``` 注意:不要加 `--enable-optimizations` 参数 # 编译并安装 ``` make && make install ``` 出现如下输出代表安装成功 ``` Installing collected packages: setuptools, pip Successfully installed pip-21.1.1 setuptools-56.0.0 ``` ### 清空缓存 如果编译错误,需要先清空缓存,再编译 ``` make clean ``` # 创建软连接 目的: - 输入 `python3` 命令,可以进入python3的环境 - 输入 `pip3` 命令,可以给python3安装库 ### 方式一 对应上面 **配置-方式一**: ``` ln -s /usr/local/bin/python3 /usr/bin/python3 ``` ### 方式二 对应上面 **配置-方式二**: ``` ln -s /usr/local/python3/bin/python3 /usr/bin/python3 ``` ``` ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 ``` # 测试 ### 进入Python3 用python3命令进入Python3 ``` python3 --version ``` ### 用pip3安装库 ``` pip3 install ``` # pip升级 ``` pip3 install --upgrade pip ``` 感谢: https://cloud.tencent.com/developer/article/1520688 原文出处:http://malaoshi.top/show_1IX3Daaf2VqX.html