1. 程式人生 > >[Linux]Ubuntu安裝pip及其各種bug解決方案

[Linux]Ubuntu安裝pip及其各種bug解決方案



原創文章,歡迎轉載。轉載請註明:轉載自 祥的部落格
原文連結:https://blog.csdn.net/humanking7/article/details/84392790


文章目錄

環境

  • 配置環境: ubuntu 16.04
  • 針對環境:python2.7.12

1. 安裝pip

apt-get install python-pip

# 當然可以把其他東西也一起安裝上
apt-get install python-pip python-dev build-essential

2. 檢視pip版本

#大寫的V,或者--version
pip -V 
# 輸出:
# pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

3. 更新pip

3.1. 正常更新

pip install --upgrade pip
#或者
pip install -U pip

但是有可能升級不了,顯示:

[email protected]:~# pip install -U pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 8.1.1
    Not uninstalling pip at /usr/lib/python2.7/dist-packages, outside environment /usr
Successfully installed pip-8.1.1
You are using pip version 8.1.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip'
command.

3.2. 更新不了,通過原始碼安裝更新

各種方法都試過,比如使用命令:python -m pip install --upgrade pip進行安裝,但是還是會出現上面的提示,所以就用原始碼進行升級。

3.2.1 下載原始碼

下載原始碼,也可以去pip官網下載 https://pypi.org/project/pip/#files

wget https://files.pythonhosted.org/packages/45/ae/8a0ad77defb7cc903f09e551d88b443304a9bd6e6f124e75c0fbbf6de8f7/pip-18.1.tar.gz
# ==========
# 詳細安裝資訊
[email protected]:~# mkdir pipDir
[email protected]:~# cd pipDir/
[email protected]:~/pipDir# wget https://files.pythonhosted.org/packages/45/ae/8a0ad77defb7cc903f09e551d88b443304a9bd6e6f124e75c0fbbf6de8f7/pip-18.1.tar.gz
--2018-11-23 13:56:53--  https://files.pythonhosted.org/packages/45/ae/8a0ad77defb7cc903f09e551d88b443304a9bd6e6f124e75c0fbbf6de8f7/pip-18.1.tar.gz
正在解析主機 files.pythonhosted.org (files.pythonhosted.org)... 2a04:4e42:1a::319, 151.101.109.63
正在連線 files.pythonhosted.org (files.pythonhosted.org)|2a04:4e42:1a::319|:443... 已連線。
已發出 HTTP 請求,正在等待迴應... 200 OK
長度: 1259370 (1.2M) [binary/octet-stream]
正在儲存至: “pip-18.1.tar.gz”

pip-18.1.tar.gz           100%[====================================>]   1.20M  9.92KB/s    in 2m 35s  

2018-11-23 13:59:31 (7.94 KB/s) - 已儲存 “pip-18.1.tar.gz” [1259370/1259370])

[email protected]:~/pipDir# ls
pip-18.1.tar.gz

3.2.2 解壓安裝

解壓安裝

# 解壓
[email protected]:~/pipDir# tar xzvf pip-18.1.tar.gz 
# 檢視解壓後的目錄
[email protected]:~/pipDir# ls
pip-18.1  pip-18.1.tar.gz
# 進入目錄 檢視裡面的檔案
[email protected]:~/pipDir# cd pip-18.1/
[email protected]:~/pipDir/pip-18.1# ls -1
AUTHORS.txt
docs
LICENSE.txt
MANIFEST.in
NEWS.rst
PKG-INFO
pyproject.toml
README.rst
setup.cfg
setup.py
src
# 安裝pip
[email protected]:~/pipDir/pip-18.1# python setup.py install
...
...

Installed /usr/local/lib/python2.7/dist-packages/pip-18.1-py2.7.egg
Processing dependencies for pip==18.1
Finished processing dependencies for pip==18.1
# 這表示安裝成功

3.2.3 更新到了最新版本,卻出現錯誤ImportError: cannot import name main

使用pip -V檢測一下版本,發現出現錯誤:

[email protected]:~/pipDir/pip-18.1# pip -V
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name main

出現問題的原因:
說是一個bug,導致只要用pip,就會丟擲一個異常,但是要解決起來也比較容易。

首先定位問題:
問題出現在了/usr/bin/中的pip檔案,用vim開啟,進行編輯:
原來的程式碼

#!/usr/bin/python
# GENERATED BY DEBIAN

import sys 

# Run the main entry point, similarly to how setuptools does it, but because
# we didn't install the actual entry point from setup.py, don't use the
# pkg_resources API.
from pip import main
if __name__ == '__main__':
    sys.exit(main())

修改後的程式碼

#!/usr/bin/python
# GENERATED BY DEBIAN

import sys 

# Run the main entry point, similarly to how setuptools does it, but because
# we didn't install the actual entry point from setup.py, don't use the
# pkg_resources API.
from pip import __main__
#from pip import main
if __name__ == '__main__':
    sys.exit(__main__._main())
    #sys.exit(main())

儲存後,去檢測一下現在pip的版本:

[email protected]:~/pipDir/pip-18.1# pip -V
pip 18.1 from /usr/local/lib/python2.7/dist-packages/pip-18.1-py2.7.egg/pip (python 2.7)

ok,已經成功更新到 pip 18.1 了。

4. 解除安裝pip

apt-get remove python-pip

5. pip常用命令

  • 檢視pip幫助:pip -help
  • 安裝新的python包:pip install packageName
  • 解除安裝python包:pip uninstall packageName
  • 尋找python包:pip search packageName
  • 檢視python2pip安裝包:pip list
  • 檢視python3pip安裝包:pip3 list

OK!
以上,Enjoy~


讚賞碼New