CentOS7下安装Python3和Py

2020-01-03 16:21:51 浏览数 (1)

(一)简述 刚安装的centos7.4版本默认是安装Python2.7,由于一些命令需要使用的原因,比如yum等,使用的是2.7.5版本的,由于现在很多库包括django都是使用Python3, 因此,有这样的需求:yum使用python2.7版本,django和其他的使用默认的python3版本。

(二)查看并备份python2相关信息 1,使用python -V 可以查看默认的版本信息。

代码语言:javascript复制
[root@localhost ~]# python -V
Python 2.7.5

2,然后使用which python 查看下python的可执行文件的位置。

代码语言:javascript复制
[root@localhost ~]# which python
/usr/bin/python

3,可执行文件在/usr/bin/目录下,查看有关python的文件,ll /usr/bin/python*。通过查看可以得知python默认指向的是python2.7

代码语言:javascript复制
[root@localhost ~]# ll /usr/bin/python*
lrwxrwxrwx. 1 root root    7 Dec 21 15:23 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root    9 Dec 21 15:23 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Aug  4  2017 /usr/bin/python2.7

重要:通过查看可以得知python默认指向的是python2.7,由于还没有安装python3,可以先备份python文件,等python3安装好了后再创建软连接即可 4,备份默认python2.7版本 [root@localhost bin]# mv /usr/bin/python /usr/bin/python.bak

(三)方法一:安装配置python3 1,安装相关的包yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

代码语言:javascript复制
[root@localhost bin]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
Loaded plugins: fastestmirror
base                                                                                                             | 3.6 kB  00:00:00     
extras                                                                                                           | 3.4 kB  00:00:00     
updates                                                                                                          | 3.4 kB  00:00:00     
Loading mirror speeds from cached hostfile
Package zlib-devel-1.2.7-17.el7.x86_64 already installed and latest version
Package 1:openssl-devel-1.0.2k-8.el7.x86_64 already installed and latest version
Package ncurses-devel-5.9-14.20130511.el7_4.x86_64 already installed and latest version
Package sqlite-devel-3.7.17-8.el7.x86_64 already installed and latest version
Package gcc-4.8.5-16.el7_4.1.x86_64 already installed and latest version
Package 1:make-3.82-23.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package bzip2-devel.x86_64 0:1.0.6-13.el7 will be installed

2,去python官网下载稳定版(https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz)

代码语言:javascript复制
[root@localhost usr]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
--2018-03-08 00:05:54--  https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
Resolving www.python.org (www.python.org)... 151.101.228.223, 2a04:4e42:12::223
Connecting to www.python.org (www.python.org)|151.101.228.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16992824 (16M) [application/octet-stream]
Saving to: ‘Python-3.6.4.tar.xz’

16% [==============>                                                                                ] 2,781,624   49.4KB/s  eta 4m 49s

3,安装python3

代码语言:javascript复制
[root@localhost tmp]# tar xf Python-3.6.1.tar.xz
[root@localhost tmp]# cd Python-3.6.1
[root@localhost Python-3.6.1]# ./configure prefix=/usr/local/python3
[root@localhost Python-3.6.1]# make &&make install

4,在/usr/bin/下创建软连接

代码语言:javascript复制
[root@localhost Python-3.6.1]# ln -s /usr/local/python3/bin/python3  /usr/bin/python
[root@localhost Python-3.6.1]# python -V
Python 3.6.1
[root@localhost Python-3.6.1]# python2 -V
Python 2.7.5
[root@localhost Python-3.6.1]# ll /usr/bin/python*
lrwxrwxrwx  1 root root   32 Mar  7 05:38 /usr/bin/python -> /usr/local/python3.6/bin/python3
lrwxrwxrwx. 1 root root    9 Jan 29 16:34 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Aug  4  2017 /usr/bin/python2.7
lrwxrwxrwx. 1 root root    7 Jan 29 16:34 /usr/bin/python2.bak -> python2

5,配置正常使用yum,使yum使用python2.7版本,否则会报错的。 5.1,修改/usr/bin/yum文件把#! /usr/bin/python修改为#! /usr/bin/python2

代码语言:javascript复制
    [root@localhost Python-3.6.1]# vim /usr/bin/yum 

#!/usr/bin/python2
代码语言:javascript复制
5.2,修改/usr/libexec/urlgrabber-ext-down文件,把里面的#! /usr/bin/python 也要修改为#! /usr/bin/python2
代码语言:javascript复制
    [root@localhost Python-3.6.1]# vim /usr/libexec/urlgrabber-ext-down 

#! /usr/bin/python2

6,如果使用pip的话,简历pip3的软连接。

代码语言:javascript复制
[root@localhost Python-3.6.1]#ln -s /usr/local/python3/bin/pip3   /usr/bin/pip3
[root@localhost Python-3.6.1]# ll /usr/bin/pip3
lrwxrwxrwx 1 root root 29 Mar  7 05:41 /usr/bin/pip3 -> /usr/local/python3.6/bin/pip3

至此,一台服务器上默认的版本是python3 ,同时python2页存在了,可以正常使用yum来进行安装所需的软件了。

(四)方法二:建立Python虚拟环境(方便快捷,推荐使用) 1,安装依赖包 [root@localhost ~]# yum -y install wget sqlite-devel xz gcc automake zlib-devel openssl-devel epel-release git 2,编译安装

代码语言:javascript复制
[root@localhost ~]# wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
[root@localhost ~]# tar xvf Python-3.6.1.tar.xz  && cd Python-3.6.1
[root@localhost ~]# ./configure && make && make install

3,建立Python虚拟环境。因为 CentOS 6/7 自带的是 Python2,而 Yum 等工具依赖原来的 Python,为了不扰乱原来的环境我们来使用 Python 虚拟环境

代码语言:javascript复制
[root@localhost ~]#cd /opt
[root@localhost ~]#python3 -m venv py3
[root@localhost ~]#source /opt/py3/bin/activate
(py3) [root@localhost ~]#

############看到下面的提示符代表成功,以后运行Python脚本都要先运行以上 source 命令,以下所有命令均在该虚拟环境中运行############

0 人点赞