CentOS 7系統同時安裝python2和python3 及對應的ipython

 

 

 

不刪除python 2.x安裝python3.xpython

CentOS7.2下默認系統自帶python2.X的版本,這個版本被系統不少程序所依賴,因此不建議刪除,若是使用最新的Python3那麼咱們知道編譯安裝源碼包和系統默認包之間是沒有任何影響的,因此能夠安裝python3和python2共存。linux

 

先檢查CentOS 7.2自帶python及pip:sql

 若是原來的python2版本下沒有pip工具,能夠先安裝Python2版本下的pip工具:bootstrap

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

 #####################################################################python3.x

(未驗證)還有一種安裝pip工具方式:先安裝setup-tools,在安裝pip:python2.7

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

 ######################################################spa

 

先安裝python 2.7版本對應的ipython,此處用pip工具安裝:code

 

提示錯誤:orm

 

安裝對應的包:

yum install python-devel libxslt-devel libffi-devel

 

再用pip安裝對應的ipython:

 

python 2.7版本對應的ipython安裝成功:

安裝python 3及對應的ipython

一、安裝依賴包

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

二、下載linux平臺的python3.x的安裝包(我下載的是python3.6版本)

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

三、解壓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

四、安裝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

五、原來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

六、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

七、對新版的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

 

 

八、使用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.

 九、修改yum配置文件

由於yum使用python2,所以替換爲python3後可能沒法正常工做,繼續使用這個python2.7.5

所以修改yum配置文件(vi /usr/bin/yum)。

把文件頭部的#!/usr/bin/python改爲#!/usr/bin/python2.7保存退出便可。

 

 十、python3安裝ipython

pip3 install ipython

能夠看到pip3安裝的ipython對應的是ipython-6.2.1版本;

 查看python3的bin目錄下ipython二進制文件,python3的bin目錄下對應的有ipython和ipython3兩個二進制文件

對python3下的ipython作軟鏈接:

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

 

至此,python3對應的ipython也安裝好了。

相關文章
相關標籤/搜索