一、Centos7安装Python3的方法
由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到。
[root@VM_105_217_centos Python-3.6.2]# pythonPython 2.7.5 (default, Aug 4 2017, 00:39:18)[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2Type "help", "copyright", "credits" or "license" for more information.
输入Python命令,查看可以得知是Python2.7.5版本
输入
which python
可以查看位置,一般是位于/usr/bin/python目录下。
下面介绍安装Python3的方法
首先安装依赖包 (我安装时没装,因为之前装了一些)
yum -y groupinstall "Development tools"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
然后根据自己需求下载不同版本的Python3,我下载的是Python3.6.4
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
如果速度不够快,可以直接去官网下载,利用WinSCP等软件传到服务器上指定位置,我的存放目录是/share/soft_backup,使用命令:
mv Python-3.6.4.tgz /share/soft_backup
然后解压压缩包,进入/share/soft_backup目录,编译安装Python3
cd /share/soft_backup/tar -zxvf Python-3.6.4.tgzcd Python-3.6.4/./configure --prefix=/share/soft/python-3.6.4make
make test #make test报了点错,但我直接make install,也没有很多问题make install
最后创建软链接
ln -s /share/soft/python-3.6.4/bin/python3 /usr/bin/python3
ln -s /share/soft/python-3.6.4/bin/pip3 /usr/bin/pip3
在命令行中输入python3测试
[root@localhost python-3.6.4]# pythonPython 2.7.14 |Anaconda, Inc.| (default, Dec 7 2017, 17:05:42) [GCC 7.2.0] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> exit()[root@localhost python-3.6.4]# [root@localhost python-3.6.4]# [root@localhost python-3.6.4]# python3Python 3.6.4 (default, Mar 7 2018, 09:54:37) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> exit()
二、这一步是没错的:给一相结合:
CentOS7中默认安装的python是2.75的,如果想要使用python3,那么还需要安装python3
第一步:更换yum的源,更换为国内的阿里云,这是为了方便安装python3的依赖
备份一下原本的yum源:
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
下载阿里云的yum源【我下的是CentOS7的,如果需要其他版本,那么只需要将下面的7改成5或6即可】【这一步需要能联网】:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
之后运行给install生成缓存
yum makecache
第二步:直接通过yum来安装python3
下载python3【我下的是3.6.2】:
wget"https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz"
解压压缩包:
tar -zxvf Python-3.6.2.tgz -C /tmp
进入目录:
cd /tmp/Python-3.6.2/
安装python3的依赖:
yum -y groupinstall"Development tools"
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
配置安装,把Python3安装到 /usr/local 目录【这里是可以自己选择安装到哪里,注意,如果不先安装依赖库,则这里会报错】:
./configure --prefix=/usr/local/python3 【这个安装路径下面的bin有python3.6命令,后面后面要使用软链接链接过来的位置。】
安装python3 :
make
make install
网友评论