CentOS7安装使用Python3,


 简介

在CentOS7上默认是安装Python2,但是现在很多程序默认都是需要Python3才能正常运行。

如果使用系统的Python环境,在系统更新的时候,更新包有可能破坏依赖。

最好的方法就是编译安装一个Python3,然后使用虚拟环境去运行我们的Python程序。

之所以使用虚拟环境就是为了让我们安装的Python3可以被多个程序使用,安装包又不会互相冲突。

编译安装

编译的时候,需要安装一些依赖包

  1. #!/bin/bash 
  2. VERSION='3.10.0' 
  3.  
  4. yum -y install xz tar gcc make tk-devel wget sqlite-devel zlib-devel  readline-devel openssl-devel curl-devel tk-devel gdbm-devel xz-devel bzip2-devel libffi-devel 
  5.  
  6. cd /root/ 
  7.  
  8. wget -c https://mirrors.huaweicloud.com/python/${VERSION}/Python-${VERSION}.tgz 
  9.  
  10. tar xvf Python-${VERSION}.tar.xz 
  11.  
  12. cd Python-${VERSION} 
  13.  
  14. ./configure --prefix=/opt/python3 
  15.  
  16. make 
  17.  
  18. make install 
  19.  
  20. echo 'export PATH=/opt/python3/bin:$PATH' >>/etc/profile 

 使用测试

  1. source /etc/profile 
  2. python3 -V 

 设置镜像源

  1. mkdir ~/.pip 
  2.  
  3. cat > ~/.pip/pip.conf <<EOF 
  4. [global] 
  5. index-url = https://repo.huaweicloud.com/repository/pypi/simple 
  6. trusted-host = repo.huaweicloud.com 
  7. timeout = 120 
  8. EOF 

 创建虚拟环境

  1. /opt/python3/bin/python3 -m pip install virtualenv 
  2. cd /opt/ 
  3. /opt/python3/bin/python3 -m virtualenv mypyhton 

这里创建的mypython就是我们的虚拟环境,使用的时候,可以使用activate模式,也可以指定路径的python执行文件

  1. source /opt/mypython/bin/activate 
  2. python -m pip list  

或者

  1. /opt/mypython/bin/python -m pip list 

 

相关内容