CentOS 7系统下不删除python2情况下安装python3 及对应的ipython

时间:2021-10-02 02:00:45

 

 

 

不删除python 2.x安装python3.x

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

 

先检查CentOS 7.2自带python及pip:

CentOS 7系统下不删除python2情况下安装python3 及对应的ipython

 如果原来的python2版本下没有pip工具,可以先安装Python2版本下的pip工具:

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
pip -V

 #####################################################################

(未验证)还有一种安装pip工具方式:先安装setup-tools,在安装pip:

wget https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg --no-check-certificate
chmod +x setuptools-0.6c11-py2.7.egg
sh setuptools-0.6c11-py2.7.egg

  再安装pip

wget https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz --no-check-certificate
cp pip-1.3.1.tar.gz /usr/src/
tar zxvf pip-1.3.1.tar.gz
cd pip-1.3.1
python setup.py install

 ######################################################

 

先安装python 2.7版本对应的ipython,此处用pip工具安装:

CentOS 7系统下不删除python2情况下安装python3 及对应的ipython

 

提示错误:

CentOS 7系统下不删除python2情况下安装python3 及对应的ipython

 

安装对应的包:

yum install python-devel libxslt-devel libffi-devel

CentOS 7系统下不删除python2情况下安装python3 及对应的ipython

 

再用pip安装对应的ipython:

CentOS 7系统下不删除python2情况下安装python3 及对应的ipython

 

python 2.7版本对应的ipython安装成功:

CentOS 7系统下不删除python2情况下安装python3 及对应的ipython

安装python 3及对应的ipython

1、安装依赖包

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

 

2、下载linux平台的python3.x的安装包(我下载的是python3.6版本)

[root@localhost ~]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz

3、解压python3.6安装包

[root@localhost ~]# tar xf Python-3.6.4.tgz 
[root@localhost ~]# ll
total 22024
-rw-------.  1 root root     1238 Jun 25 10:54 anaconda-ks.cfg
drwxr-xr-x. 17  501  501     4096 Mar 21 03:47 Python-3.6.4
-rw-r--r--.  1 root root 22540566 Mar 21 03:47 Python-3.6.4.tgz

4、安装python3.6,安装路径为/usr/local/python3

#配置安装路径
[root@localhost]# mkdir /usr/local/python3 [root@localhost Python-3.6.4]# ./configure --prefix=/usr/local/python3 #安装python3.6 [root@localhost Python-3.6.4]# make && make install

 

5、原来python2和新的python3

[root@bogon Python-3.6.4]# which python
/usr/bin/python
[root@bogon Python-3.6.4]# python -V
Python 2.7.5
[root@bogon Python-3.6.4]# /usr/bin/python -V
Python 2.7.5
[root@bogon Python-3.6.4]# /usr/local/python3/bin/python3
python3 python3.6 python3.6-config python3.6m python3.6m-config python3-config
[root@bogon Python-3.6.4]# /usr/local/python3/bin/python3 -V
Python 3.6.4

 

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

[root@localhost ~]# ll /usr/local/python3/bin
total 24656
lrwxrwxrwx. 1 root root        8 Jun 25 11:22 2to3 -> 2to3-3.6
-rwxr-xr-x. 1 root root      101 Jun 25 11:22 2to3-3.6
-rwxr-xr-x. 1 root root      242 Jun 25 11:22 easy_install-3.6
lrwxrwxrwx. 1 root root        7 Jun 25 11:22 idle3 -> idle3.6
-rwxr-xr-x. 1 root root       99 Jun 25 11:22 idle3.6
-rwxr-xr-x. 1 root root      214 Jun 25 11:22 pip3
-rwxr-xr-x. 1 root root      214 Jun 25 11:22 pip3.6
lrwxrwxrwx. 1 root root        8 Jun 25 11:22 pydoc3 -> pydoc3.6
-rwxr-xr-x. 1 root root       84 Jun 25 11:22 pydoc3.6
lrwxrwxrwx. 1 root root        9 Jun 25 11:22 python3 -> python3.6
-rwxr-xr-x. 2 root root 12604536 Jun 25 11:21 python3.6
lrwxrwxrwx. 1 root root       17 Jun 25 11:22 python3.6-config -> python3.6m-config
-rwxr-xr-x. 2 root root 12604536 Jun 25 11:21 python3.6m
-rwxr-xr-x. 1 root root     3097 Jun 25 11:22 python3.6m-config
lrwxrwxrwx. 1 root root       16 Jun 25 11:22 python3-config -> python3.6-config
lrwxrwxrwx. 1 root root       10 Jun 25 11:22 pyvenv -> pyvenv-3.6
-rwxr-xr-x. 1 root root      441 Jun 25 11:22 pyvenv-3.6

7、对新版的python及对应的pip做软链接

ln -sv /usr/local/python3/bin/python3 /usr/bin/python3
ln -sv /usr/local/python3/bin/pip3 /usr/bin/pip3
[root@localhost ~]# which python3
/usr/bin/python3
[root@localhost Python-3.6.4]# pip3 -V
pip 9.0.1 from /usr/local/python3/lib/python3.6/site-packages (python 3.6)
[root@localhost Python-3.6.4]# python3 -V
Python 3.6.4

 

CentOS 7系统下不删除python2情况下安装python3 及对应的ipython

 

8、使用python3命令查看版本

[root@localhost ~]# python3 -V
Python 3.6.4    
[root@localhost local ~]# python3
Python 3.6.4 (default, Jun 25 2017, 11:20:12) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.

 9、修改yum配置文件

因为yum使用python2,因此替换为python3后可能无法正常工作,继续使用这个python2.7.5

因此修改yum配置文件(vi /usr/bin/yum)。

把文件头部的#!/usr/bin/python改成#!/usr/bin/python2.7保存退出即可。

 

 10、python3安装ipython

pip3 install ipython

可以看到pip3安装的ipython对应的是ipython-6.2.1版本;

CentOS 7系统下不删除python2情况下安装python3 及对应的ipython

 查看python3的bin目录下ipython二进制文件,python3的bin目录下对应的有ipython和ipython3两个二进制文件

CentOS 7系统下不删除python2情况下安装python3 及对应的ipython

对python3下的ipython做软连接:

ln -sv /usr/local/python3/bin/ipython3 /usr/bin/ipython3

CentOS 7系统下不删除python2情况下安装python3 及对应的ipython

 

至此,python3对应的ipython也安装好了。

CentOS 7系统下不删除python2情况下安装python3 及对应的ipython