原文地址:http://www.omgdba.com/python-use-pypi-manage-packages.html
PyPI(Python Pakkage Index,http://pypi.python.org/pypi)是一個Python的公用資產庫,它管理者Python相關的哥哥功能包。任何人都可以登入該首頁去下載Python的相關包資源。當然,為了方便起見,它也提供一個名叫pip的軟體來和easy_install配合來管理組件資源,只用簡單的命令就可以對包進行安裝、更新以及刪除包。
1. pip的安裝
pip可正常工作在Windows、Mac OS、Unix/Linux等作業系統上,但是需要至少2.6+和3.2+的CPython或PyPy的支援。
python 2.7.9 和3.4以後的版本已經內建累pip程式,所以不需要安裝。其他的版本可以使用以下命令進行安裝:
| 1 2 3 4 5 6 |
#下載 https : / / bootstrap .pypa .io / get - pip .py #安裝(Linux下需要管理員權限) python get - pip .py #加入環境變數(Windows) #將C:\Python27\Scripts加入PATH |
更多的安裝方法請參照:https://pip.pypa.io/en/latest/installing.html
2. pip的使用
| 1 2 3 4 5 6 7 8 |
#安裝包 $ pip install SomePackage # 最新版本 $ pip install SomePackage == 1.0.4 # 指定版本 $ pip install 'SomePackage>=1.0.4' # 最小版本 #更新包 $ pip install - U SamePackage #刪除包 $ pip uninstall SomePackage |
3. 使用第三方源進行更新
因為某些網路原因,在串連pip官網的時候狀況不是很穩定。幸好有很多鏡像站可以使用。我們可以用鏡像站加速下載。鏡像站列表:http://www.pypi-mirrors.org/
使用鏡像源很簡單,用-i指定就行了:
easy_install -i http://pypi.douban.com/simple/ gevent
pip install -i http://pypi.douban.com/simple/ gevent
如果覺得太麻煩,可以配製成預設:
linux下,修改~/.pip/pip.conf,如果沒這檔案則建立。
windows下,修改%HOMEPATH%\pip\pip.ini。
內容為:
[global]
index-url = http://pypi.douban.com/simple
新的版本中會在非https傳輸的鏡像站上報出如下錯誤:
This repository located at mirrors.aliyun.com is not a trusted host, if this repository is available via HTTPS it is recommend to use HTTPS instead, otherwise you may silence this warning with ‘–trusted-host mirrors.aliyun.com’.
DEPRECATION: Implicitly allowing locations which are not hosted at a secure origin is deprecated and will require the use of –trusted-host in the future.
Requirement already up-to-date: pip in /usr/lib/python2.7/site-packages
解決辦法就是: