废话不多说,直接上代码:
1、首先查看python版本
whereis python
2、说明一下,linux中,基本上的软件后者软链接(类似windows下的快捷方式)都在/usr/bin/目录中。现在我们切换到该目录中查看python应用或者软链接对应的应用
cd /usr/bin/
ll python*
如果查看没有python3的相关内容则说明该centos7未安装python3
接下来我们进行安装python所需的依赖环境
yum install -y zlib zlib-devel libaio net-tools bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel python3-devel tk-devel gcc cmake gcc-c++ make libffi-devel mesa-libGL.x86_64 wget git
3、下载python3.7.8的源码包
wget https://www.python.org/ftp/python/3.7.8/Python-3.7.8.tar.xz
4、编译解压
tar -xxf Python-3.7.8.tar.xz
5、手动编译python3
cd Python-3.7.8
./configure --prefix=/usr/local/python378 --enable-shared
make && make install
6、添加软连接
ln -s /usr/local/python378/bin/python3.7 /usr/bin/python378
将依赖文件移动到/usr/lib64下,否则python运行不起来
cp /home/Python-3.7.8/libpython3.7m.so.1.0 /usr/lib64/libpython3.7m.so.1.0
7、测试安装
python378 -V
8、安装python3的pip(如果包不完整的情况下可能需要安装,否则在python的安装目录bin下面有pip的)
wget https://bootstrap.pypa.io/get-pip.py
9、使用python3安装(如果存在可以忽略此步骤)
python378 get-pip.py
10、添加pip3的软链接到/usr/bin/ (如果存在可以忽略此步骤)
ln -s /usr/local/python378/bin/pip3 /usr/bin/pip378
11、测试pip
pip378 -V
12、python安装虚拟环境 virtualenv
pip378 install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com virtualenv
pip378 install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com virtualenvwrapper
13、创建一个虚拟环境(虚拟环境名称是test)
virtualenv -p /usr/bin/python378 test
14、进入虚拟环境命令(source 到刚刚创建的环境目录下的bin/activate)
source test/bin/activate
15、退出虚拟环境
deactivate
ok,至此已经全部完成安装,可以尽情使用啦,出此教程只为自己记录,能够给大家提供帮助也最好。
特别感谢:
python3安装教程参照
centos7安装python3保姆教程_还卿一钵无情泪的博客-CSDN博客_centos7安装pythonhttps://blog.csdn.net/weixin_48185819/article/details/122586200
pip3安装教程参照
Centos7系统python3及pip安装 - Mr.小柒 - 博客园https://www.cnblogs.com/cfy930721/p/14971635.html
附加一个 阿里云源:更换地址在/etc/yum.repos.d文件加下:
cd /etc/yum.repos.d
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/BaseOS/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/BaseOS/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/os/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[PowerTools]
name=CentOS-$releasever - PowerTools - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/PowerTools/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/PowerTools/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/PowerTools/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[AppStream]
name=CentOS-$releasever - AppStream - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/AppStream/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/AppStream/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/AppStream/$basearch/os/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official