CentOS 7.5下安装Python 3.x与原有Python 2.x共存

Linux下默认系统自带Python2.X的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3那么我们知道编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装Python3和Python2共存。

1、下载Linux平台的Python3.x的安装包(本文测试安装下载的是Python 3.7版本)

CentOS 7.5下安装Python 3.x与原有Python 2.x共存

2、解压python3.6安装包

CentOS 7.5下安装Python 3.x与原有Python 2.x共存

3、安装Python3.7,默认安装路径为/usr/local

--prefix=PREFIX install architecture-independent files in PREFIX
  [/usr/local]

#安装报错,缺失依赖库
[linuxidc@localhost Python-3.7.2]$ ./configure
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.7... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for a sed that does not truncate output... /usr/bin/sed
checking for --with-cxx-main=<compiler>... no
checking for g++... no

.......

如果您想要一个具有稳定优化的发布版本(PGO等),
请运行./configure --enable-optimizations

CentOS 7.5下安装Python 3.x与原有Python 2.x共存

#如果有问题请安装依赖库
[linuxidc@localhost Python-3.7.2]$ yum install gcc

#安装python3.7
[linuxidc@localhost Python-3.7.2]$ make && make install

CentOS 7.5下安装Python 3.x与原有Python 2.x共存

#make && make install 报错
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1

#安装zlib-devel
[root@localhost Python-3.7.2]# yum install zlib-devel

如果出现:

ModuleNotFoundError: No module named '_ctypes'
make: *** [install] 错误 1

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

#yum install libffi-devel -y    《-安装这个就能解决

#重新安装
[root@localhost Python-3.7.2]# make && make install

4、Python3.7已安装成功,在/usr/local/bin下生成命令python3

CentOS 7.5下安装Python 3.x与原有Python 2.x共存

5、使用Python3命令查看版本

CentOS 7.5下安装Python 3.x与原有Python 2.x共存

6、更换系统默认的Python版本

备份或删除Python2.x

mv  /usr/bin/python  /usr/bin/python2.7

新建指向新版本的Python3.x和pip3的软连接

ln  -s  /usr/local/python3/bin/python3.7  /usr/bin/python

ln  -s  /usr/local/python3/bin/pip3  /usr/bin/pip

如何利用pip将python模块安装到指定的python版本中

问题
如电脑上同时装了python2(2.7)和python3(3.7),当使用pip安装时默认应安装到python2中,pip3安装时应安装到python3中,但奇怪的是使用pip安装时每次都定位到python3中,不知是啥原因,也不知如何将其重定向到python2中,索性手动指定pip到python2中

查看pip版本
[root@localhost ipython]# pip -V
pip 19.0.3 from /usr/lib/python2.7/site-packages/pip (python 2.7)
[root@localhost ipython]# pip2 -V
pip 19.0.3 from /usr/lib/python2.7/site-packages/pip (python 2.7)

CentOS 7.5下安装Python 3.x与原有Python 2.x共存

pip指定python版本安装
安装到python2.7版本中:sudo pip2 install 模块名 或 python2 -m pip install 模块名
安装到python3.5版本中:sudo pip3 install 模块名 或 python3 -m pip install 模块名

修改yum相关设置

因yum的功能依赖于Python2.x,更改python默认版本后会导致 yum无法正常工作,所以要修改yum

vi  /usr/bin/yum

修改第一行

#!/usr/bin/python2.7

相关推荐