經(jīng)常在使用Python的時候需要安裝各種模塊,而pip是很強大的模塊安裝工具,但是由于國外官方pypi經(jīng)常被墻,導(dǎo)致不可用,所以我們最好是將自己使用的pip源更換一下,這樣就能解決被墻導(dǎo)致的裝不上庫的煩惱。
網(wǎng)上有很多可用的源,例如豆瓣:http://pypi.douban.com/simple/
清華:https://pypi.tuna.tsinghua.edu.cn/simple
最近使用得比較多并且比較順手的是清華大學(xué)的pip源,它是官網(wǎng)pypi的鏡像,每隔5分鐘同步一次,地址為 https://pypi.tuna.tsinghua.edu.cn/simple
臨時使用:
可以在使用pip的時候加參數(shù):-i https://pypi.tuna.tsinghua.edu.cn/simple
例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple gevent,這樣就會從清華這邊的鏡像去安裝gevent庫。
永久修改,一勞永逸:
linux下,修改 ~/.pip/pip.conf (沒有就創(chuàng)建一個), 修改 index-url,內(nèi)容如下:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
windows下,在python安裝目錄中$PYTHON_HOME\Lib\site-packages\pip\models,修改index.py內(nèi)容如下
PyPI = Index(‘https://pypi.python.org/‘)
修改為:
PyPI = Index(‘https://pypi.tuna.tsinghua.edu.cn/‘)