CentOS7安装升级Python3

本文详细描述了在CentOS7系统中从默认版本升级到Python3.6,通过yum和手动编译安装Python3.7,以及设置软连接、修改PATH环境变量和升级pip的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CentOS7安装升级Python3
一、查看默认的Python版本
默认安装的python2
[root@server ~]# python
Python 2.7.5 (default, Oct 14 2020, 14:45:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@server ~]# python3
bash: python3: 未找到命令...
相似命令是: 'python'
[root@server ~]# pip -V
bash: pip: 未找到命令..
[root@server ~]# pip3 -V
bash: pip3: 未找到命令...
[root@server ~]#.
[root@server ~]#
[root@server ~]# ll -lsh /usr/bin/python*
   0 lrwxrwxrwx. 1 root root    7 1月   8 2022 /usr/bin/python -> python2
   0 lrwxrwxrwx. 1 root root    9 1月   8 2022 /usr/bin/python2 -> python2.7
8.0K -rwxr-xr-x. 1 root root 7.0K 10月 14 2020 /usr/bin/python2.7
[root@server ~]#

二、yum方式安装的python3
[root@server ~]# yum install python3

[root@server ~]# pip -V
bash: pip: 未找到命令...
[root@server ~]# pip3 -V
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
[root@server ~]# python3
Python 3.6.8 (default, Nov 14 2023, 16:29:52)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

查看软连接
[root@server ~]# ll -lsh /usr/bin/python*
   0 lrwxrwxrwx. 1 root root    7 1月   8 2022 /usr/bin/python -> python2
   0 lrwxrwxrwx. 1 root root    9 1月   8 2022 /usr/bin/python2 -> python2.7
8.0K -rwxr-xr-x. 1 root root 7.0K 10月 14 2020 /usr/bin/python2.7
   0 lrwxrwxrwx  1 root root    9 1月  12 15:16 /usr/bin/python3 -> python3.6
 12K -rwxr-xr-x  2 root root  12K 11月 15 00:31 /usr/bin/python3.6
 12K -rwxr-xr-x  2 root root  12K 11月 15 00:31 /usr/bin/python3.6m
[root@server ~]#

三、编译安装更高版本的python3
[root@server ~]# tar zxvf Python-3.7.0.tgz
[root@server ~]# cd Python-3.7.0/
[root@server Python-3.7.0]# ls
aclocal.m4    configure     Grammar     Lib      Makefile.pre.in  Objects  PCbuild        Python      Tools
config.guess  configure.ac  Include     LICENSE  Misc             Parser   Programs       README.rst
config.sub    Doc           install-sh  Mac      Modules          PC       pyconfig.h.in  setup.py
[root@server Python-3.7.0]# ./configure --prefix=/usr/local/python3

// 执行完会到最后会提示如下信息。
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations


// 安装前先安装一下依赖包,已安装忽略
[root@server Python-3.7.0]# yum install libffi-devel zlib* -y
//3.7版本需要安装libffi-devel包否则报错,Module Not Found Error: No module named '_ctypes'


[root@server Python-3.7.0]# ./configure --prefix=/usr/local/python3 --enable-optimizations
[root@server Python-3.7.0]# make && make install

四、创建软连接
[root@server Python-3.7.0]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln: 无法创建符号链接"/usr/bin/python3": 文件已存在
[root@server Python-3.7.0]#

[root@server ~]# rm -rf /usr/bin/python3
[root@server ~]# ll -lsh /usr/bin/python*
   0 lrwxrwxrwx. 1 root root    7 1月   8 2022 /usr/bin/python -> python2
   0 lrwxrwxrwx. 1 root root    9 1月   8 2022 /usr/bin/python2 -> python2.7
8.0K -rwxr-xr-x. 1 root root 7.0K 10月 14 2020 /usr/bin/python2.7
 12K -rwxr-xr-x  2 root root  12K 11月 15 00:31 /usr/bin/python3.6
 12K -rwxr-xr-x  2 root root  12K 11月 15 00:31 /usr/bin/python3.6m
[root@server ~]#
[root@server ~]# python3
Python 3.6.8 (default, Nov 14 2023, 16:29:52)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@server ~]#

[root@server Python-3.7.0]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@server Python-3.7.0]#
[root@server ~]# ll -lsh /usr/bin/python*
   0 lrwxrwxrwx. 1 root root    7 1月   8 2022 /usr/bin/python -> python2
   0 lrwxrwxrwx. 1 root root    9 1月   8 2022 /usr/bin/python2 -> python2.7
8.0K -rwxr-xr-x. 1 root root 7.0K 10月 14 2020 /usr/bin/python2.7
   0 lrwxrwxrwx  1 root root   30 1月  12 17:30 /usr/bin/python3 -> /usr/local/python3/bin/python3
 12K -rwxr-xr-x  2 root root  12K 11月 15 00:31 /usr/bin/python3.6
 12K -rwxr-xr-x  2 root root  12K 11月 15 00:31 /usr/bin/python3.6m
[root@server ~]#

查看新版本
[root@server ~]# python3
Python 3.7.0 (default, Jan 12 2024, 17:25:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>


五、添加到PATH
vim ~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

#PATH=$PATH:$HOME/bin
PATH=$PATH:$HOME/bin:/usr/local/python3/bin

export PATH

//修改生效
source ~/.bash_profile

六、检查python3和pip,升级pip,有可能网速卡,多试几次
[root@server ~]# python3 -V
Python 3.7.0
[root@server ~]#
[root@server Python-3.7.0]# pip3 -V
pip 10.0.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)
[root@server ~]#


[root@server Python-3.7.0]# pip3 install --upgrade pip
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)")': /simple/pip/
Collecting pip

  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fa0937f0ac8>: Failed to establish a newconnection: [Errno 101] 网络不可达')':
/packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl

  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fa0937f05c0>: Failed to establish a newconnection: [Errno 101] 网络不可达')': /packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl

  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl

  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl

  Downloading https://files.pythonhosted.org/packages/15/aa/3f4c7bcee2057a76562a5b33ecbd199be08cdb4443a02e26bd2c3cf6fc39/pip-23.3.2-py3-none-any.whl (2.1MB)
    100% |████████████████████████████████| 2.1MB 11kB/s
Installing collected packages: pip
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
      Successfully uninstalled pip-10.0.1
Successfully installed pip-23.3.2
[root@server Python-3.7.0]#

[root@server Python-3.7.0]# pip3 -V
pip 23.3.2 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)
[root@server Python-3.7.0]#

### 在 CentOS 7安装 Python 3 的方法 在 CentOS 7安装 Python 3 可以通过多种方式进行,以下是具体的操作步骤和相关信息: #### 方法一:使用 `yum` 安装 Python 3 CentOS 7 默认的软件仓库可能只包含较旧版本的 Python。为了安装更高版本的 Python 3,可以启用 EPEL 和 IUS 存储库。 1. 安装 EPEL 存储库: ```bash sudo yum install epel-release ``` 2. 安装 IUS 存储库: ```bash sudo yum install https://repo.ius.io/ius-release-el7.rpm ``` 3. 安装 Python 3: ```bash sudo yum install python36u ``` 4. 验证安装: ```bash python3.6 --version ``` 此方法适合快速安装官方支持的 Python 版本[^2]。 #### 方法二:从源代码编译安装 Python 3 如果需要安装最新版本的 Python(例如 Python 3.9),可以从源代码进行编译和安装。 1. 安装依赖包: ```bash sudo yum groupinstall "Development Tools" sudo yum install gcc make wget zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel xz-devel ``` 2. 下载 Python 源码包: ```bash cd /usr/src sudo wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz ``` 3. 解压并编译: ```bash sudo tar xzf Python-3.9.6.tgz cd Python-3.9.6 sudo ./configure --enable-optimizations sudo make altinstall ``` 注意:使用 `make altinstall` 而不是 `make install`,以避免覆盖系统默认的 `python` 命令[^3]。 4. 验证安装: ```bash python3.9 --version ``` #### 方法三:删除 Python 2 并安装 Python 3(不推荐) 如果希望完全移除 Python 2 并替换为 Python 3,请谨慎操作,因为某些系统工具可能依赖于 Python 2。 1. 删除 Python 2: ```bash rpm -qa | grep python2 | xargs rpm -ev --allmatches --nodeps whereis python2 | xargs rm -frv ``` 2. 安装 Python 3: 使用方法一或方法二完成安装[^5]。 #### 安装 pip 包管理工具 无论使用哪种方法安装 Python 3,都可以通过以下命令安装升级 `pip`: ```bash sudo yum install python3-pip pip3 install --upgrade pip ``` 验证 `pip` 是否安装成功: ```bash pip3 --version ``` --- ### 注意事项 - 如果计划在团队开发中使用 Python 环境,建议参考其他操作系统上的本地编程环境指南[^4]。 - 在生产环境中,确保测试新版本的 Python 对现有应用程序的影响。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值