Centos7下python2.x 和python3.x共存

摘要

目前centos7系统自带的Python版本是2.7.5,这个版本呗系统中的很多程序锁依赖,所有我们不可能去删除它。如果这个时候我们还想用python3.x 版本,其实这里有两个方式,一个是利用pyenv 创建一个纯净的python3.x 环境,可以参考Centos 下 python 纯净虚拟环境;另外一个就是这里介绍的。

下载

root@pts/0 $ wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
--2018-01-11 17:13:58--  https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
正在解析主机 www.python.org (www.python.org)... 151.101.228.223, 2a04:4e42:36::223
正在连接 www.python.org (www.python.org)|151.101.228.223|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:22540566 (21M) [application/octet-stream]
正在保存至: “Python-3.6.1.tgz”

100%[=========================================================================================>] 22,540,566  3.34MB/s 用时 7.8s

2018-01-11 17:14:06 (2.74 MB/s) - 已保存 “Python-3.6.1.tgz” [22540566/22540566])

依赖

正式安装之前,需要先安装一些依赖保证后续的安装正常进行

yum install -y gcc gcc-c++ autoconf automake zlib-devel

安装

# 解压
root@pts/0 $ tar -zxf Python-3.6.1.tgz
root@pts/0 $ ll
总用量 26112
drwxr-xr-x 17  501  501     4096 321 2017 Python-3.6.1
-rw-r--r--  1 root root 22540566 321 2017 Python-3.6.1.tgz

# 编译
默认会被安装到 /usr/local下面。这里我们通过configure  的--prefix参数自定义安装路径,方便以后的卸载

root@pts/0 $ cd Python-3.6.1
root@pts/0 $  ./configure --prefix=/usr/local/python3
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for python3.6... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... linux
checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no

# 安装
root@pts/1 $ make && make install

验证

root@pts/1 $ ls -l /usr/local/python3/
总用量 16
drwxr-xr-x 2 root root 4096 111 17:09 bin
drwxr-xr-x 3 root root 4096 111 17:09 include
drwxr-xr-x 4 root root 4096 111 17:09 lib
drwxr-xr-x 3 root root 4096 111 17:09 share

root@pts/1 $ /usr/local/python3/bin/python3 -V
Python 3.6.1

root@pts/1 $ /usr/local/python3/bin/python3
Python 3.6.1 (default, Jan 11 2018, 17:08:20)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
### 安装准备 为了在 CentOS 8 上成功安装 Python 3.9.9,需先确保系统已更新并安装必要的开发工具库。这可以通过执行 `yum` 更新命令以及安装所需的依赖包来完成[^2]。 ```bash sudo dnf update -y sudo dnf install gcc openssl-devel bzip2-devel libffi-devel zlib-devel -y ``` ### 下载与解压源码 从官方站点下载指定版本的 Python 源代码压缩文件,并将其解压到本地目录中[^1]。 ```bash wget https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tgz tar -zxvf Python-3.9.9.tgz cd Python-3.9.9 ``` ### 编辑配置文件 (可选) 如果遇到特定模块无法正常工作的情况,可以编辑 `Modules/Setup` 文件以启用或禁用某些特性。不过对于大多数情况,默认设置已经足够满足需求。 ### 配置编译选项 使用带有优化参数的 configure 脚本来设定构建环境变量,同时指定了共享库路径以便于后续链接操作。需要注意的是,在此过程中应采用英文状态下的双引号包裹字符串内容。 ```bash ./configure --enable-optimizations --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib" ``` ### 执行编译过程 通过调用 `make altinstall` 来代替传统的 `make install` 可防止覆盖现有的默认 Python 解释器版本。这样做能够保持系统的稳定性,同时也允许在同一台机器上共存多个不同版本的解释器实例。 ```bash make altinstall ``` ### 设置环境变量 为了让新安装的 Python 版本可以在终端中被直接访问,需要修改用户的 shell 初始化脚本(如 `.bashrc`),向 `$PATH` 中添加 `/usr/local/bin` 的绝对路径。同样要注意这里的引号也应该是英文输入法下的一对直角引号。 ```bash echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bashrc source ~/.bashrc ``` 最后验证安装是否成功: ```bash python3.9 --version ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值