centos7 python2和python3共存


一、解決Python2 pip問題

  在centos7中安裝好操作系統,自帶的是Python2的版本,但是並沒有pip的方法,我們需要自行安裝 報名為python-pip

# 默認python2的版本
[root@operation ~]# python
Python 2.7.5 (default, Aug  4 2017, 00:39:18) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

# 安裝Python2的pip
[root@operation ~]# yum install epel-release -y
[root@operation ~]# yum -y install python-pip

# 安裝完成后不是最新的pip版本要進行升級
[root@operation ~]# pip install --upgrade pip

# 測試
[root@operation ~]# pip -V(大寫V)
pip 18.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)

# 現在可以使用pip進行對Python2 進行安裝Python包了
# 第一種方法:
[root@operation ~]# pip install 包名

# 第二種方法:
[root@operation ~]# python -m pip install pymongo  (安裝Python2的包)

# 若是安裝的Python3
[root@operation ~]# python3 -m pip install pymongo (安裝Python3的包)

  

二、安裝Python3

  安裝依賴關系

[root@operation ~]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

注:不能忽略相關包,我之前就沒有安裝readline-devel導致執行python模式無法使用鍵盤的上下左右鍵;

  下載源碼包

[root@operation ~]# wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz

注:如果沒有wget命令可以使用 yum -y install wget 安裝
注:我這里安裝的是3.6.6的Python版本 如果想要安裝其他的版本可以直接修改版本號

  解壓、編譯、安裝

# 解壓
[root@operation ~]# tar -xvJf Python-3.6.6.tar.xz 

# 編譯
[root@operation ~]# cd Python-3.6.6
[root@operation Python-3.6.6]# ./configure prefix=/usr/local/python3

# 安裝
[root@operation Python-3.6.6]# make && make install

注:沒有報錯及安裝成功,如果報錯可以看看是不是一些依賴包沒有安裝 自行解決不了可以留言評論或者直接聯系我

  設置軟連接

# 安裝完成還是不可以直接在終端輸入python3 進入編譯器的,我們需要設置軟鏈接
[root@operation Python-3.6.6]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3

# 這樣直接執行Python3 就可以進入Python3版本的解釋器了
[root@operation Python-3.6.6]# python3
Python 3.6.6 (default, Oct 12 2018, 12:02:11) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

  配置Python3的pip

# 設置完python執行后 python3的pip還是不能的用的,也是需要設置的軟鏈接才可以的,在python3的解壓目錄下是有pip3的命令的
[root@operation Python-3.6.6]# cd /usr/local/python3/bin/
[root@operation bin]# ll pip*
-rwxr-xr-x 1 root root 232 10月 12 12:08 pip
-rwxr-xr-x 1 root root 232 10月 12 12:08 pip3
-rwxr-xr-x 1 root root 232 10月 12 12:08 pip3.6

# 我們需要做個軟鏈接即可
[root@operation bin]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

# 安裝完成后不是最新的pip3版本要進行升級
[root@operation ~]# pip3 install --upgrade pip

  測試

# 測試
[root@operation bin]# pip3 -V
pip 18.1 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)

# 使用
[root@operation bin]# pip3 install 包名

或者
[root@operation bin]# python3 -m pip install 包名

  

三、安裝TAB補全的解釋器(ipython)

  安裝(我這里安裝雙版本的)

# 安裝Python2的ipython
# 第一種方法
[root@operation ~]# pip install ipython
# 第二種方法
[root@operation ~]# python -m pip install ipython

# 安裝Python3的ipython
# 第一種方法
[root@operation ~]# pip3 install ipython
# 第二種方法
[root@operation ~]# python3 -m pip install ipython

注:安裝無報錯安裝成功

  雙版本設置軟鏈接

# 因為是安裝了Python的雙版本而且安裝的包名都叫 ipython 所有我們執行ipython的時候使用的是安裝的python2的版本,我們要使用雙版本就要使用軟鏈接

[root@operation ~]# ipython
Python 2.7.5 (default, Aug  4 2017, 00:39:18) 
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的ipython 使用軟鏈接
[root@operation ~]# ln -s /usr/local/python3/bin/ipython /usr/bin/ipython3

  測試

# Python2的ipython
[root@operation ~]# ipython
Python 2.7.5 (default, Aug  4 2017, 00:39:18) 
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的ipython
[root@operation ~]# ipython3
Python 3.6.6 (default, Oct 12 2018, 12:02:11) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]:  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM