CentOS7下默认系统自带python2.X的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3那么我们知道编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装python3和python2共存。
查看centos7自带python版本
[root@localhost ~]# python -V Python 2.7.5 [root@localhost ~]# pip -V pip 19.0.3 from /usr/lib/python2.7/site-packages/pip (python 2.7)
如果原来的python2版本下没有pip工具,可以先安装Python2版本下的pip工具:
wget https://bootstrap.pypa.io/get-pip.py python get-pip.py pip -V
安装python 2.7版本对应的ipython,此处用pip工具安装:
[root@localhost ~]# pip install ipython
报错,安装对应的包
yum install python-devel libxslt-devel libffi-devel
安装完成,查看ipython
[root@localhost ~]# ipython -V 5.8.0 [root@localhost ~]# ipython Python 2.7.5 (default, Oct 30 2018, 23:45:53) Type "copyright", "credits" or "license" for more information. IPython 5.8.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]:
安装python3
[root@localhost src]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz [root@localhost src]# tar xf Python-3.6.4.tgz [root@localhost src]# cd Python-3.6.4 [root@localhost Python-3.6.4]# ./configure --prefix=/usr/local/python3 [root@localhost Python-3.6.4]# make && make install
创建软连接
ln -sv /usr/local/python3/bin/python3 /usr/bin/python3 ln -sv /usr/local/python3/bin/pip3 /usr/bin/pip3
查看
[root@localhost ~]# pip3 -V pip 9.0.1 from /usr/local/python3/lib/python3.6/site-packages (python 3.6) [root@localhost ~]# python3 -V Python 3.6.4
python3安装ipython
[root@localhost ~]# pip3 install ipython
创建软链接
ln -sv /usr/local/python3/bin/ipython3 /usr/bin/ipython3
查看
[root@localhost ~]# ipython -V 5.8.0 [root@localhost ~]# ipython3 -V 7.4.0
因为yum使用python2,因此替换为python3后可能无法正常工作,继续使用这个python2.7.5
因此修改yum配置文件(vi /usr/bin/yum)。
把文件头部的#!/usr/bin/python改成#!/usr/bin/python2.7保存退出即可。