红联Linux门户
Linux帮助

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

发布时间:2019-05-23 00:00:00来源:未知作者:admin

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

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

[linuxidc@localhost Linux公社 www.linuxidc.com]$ wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz

2、解压python3.6安装包

[linuxidc@localhost Linux公社 www.linuxidc.com]$ tar xf  Python-3.7.2.tgz
[linuxidc@localhost Linux公社 www.linuxidc.com]$ ll
总用量 22368
drwxrwxr-x  2 linuxidc linuxidc      30 2月  23 17:24 Linux公社
drwxr-xr-x 18 linuxidc linuxidc    4096 12月 24 11:41 Python-3.7.2
-rwxrw-rw-  1 linuxidc linuxidc 22897802 2月  23 19:04 Python-3.7.2.tgz

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

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

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

#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

[linuxidc@localhost linuxidc.com]$ ll /usr/local/bin/
总用量 27376
lrwxrwxrwx 1 root root        8 2月  23 19:25 2to3 -> 2to3-3.7
-rwxr-xr-x 1 root root      101 2月  23 19:25 2to3-3.7
lrwxrwxrwx 1 root root        8 2月  23 19:25 pydoc3 -> pydoc3.7
-rwxr-xr-x 1 root root      84 2月  23 19:25 pydoc3.7
lrwxrwxrwx 1 root root        9 2月  23 19:25 python3 -> python3.7
-rwxr-xr-x 2 root root 13979432 2月  23 19:23 python3.7
lrwxrwxrwx 1 root root      17 2月  23 19:25 python3.7-config -> python3.7m-config
-rwxr-xr-x 2 root root 13979432 2月  23 19:23 python3.7m
-rwxr-xr-x 1 root root    3097 2月  23 19:25 python3.7m-config
lrwxrwxrwx 1 root root      16 2月  23 19:25 python3-config -> python3.7-config
lrwxrwxrwx 1 root root      10 2月  23 19:25 pyvenv -> pyvenv-3.7
-rwxr-xr-x 1 root root      441 2月  23 19:25 pyvenv-3.7
lrwxrwxrwx 1 root root        6 2月  22 19:13 stream -> magick

5、使用Python3命令查看版本

[linuxidc@localhost linuxidc.com]$ python3 -V
Python 3.7.2
[linuxidc@localhost linuxidc.com]$ python3
Python 3.7.2 (default, Feb 23 2019, 19:19:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

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)

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

更多Python相关信息见Python 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=17