經常在使用Python的時候需要安裝各種模塊,而pip是很強大的模塊安裝工具,但是由于國外官方pypi經常被墻,導致不可用,所以我們最好是將自己使用的pip源更換一下,這樣就能解決被墻導致的裝不上庫的煩惱。
網上有很多可用的源,例如豆瓣:http://pypi.douban.com/simple/
清華:https://pypi.tuna.tsinghua.edu.cn/simple
最近使用得比較多并且比較順手的是清華大學的pip源,它是官網pypi的鏡像,每隔5分鐘同步一次,地址為 https://pypi.tuna.tsinghua.edu.cn/simple
臨時使用:
可以在使用pip的時候加參數-i https://pypi.tuna.tsinghua.edu.cn/simple
例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pandas,這樣就會從清華這邊的鏡像去安裝pandas庫。
永久修改,一勞永逸:
linux下,修改 ~/.pip/pip.conf (沒有就創建一個), 修改 index-url至tuna,內容如下:
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple
windows下,直接在user目錄中創建一個pip目錄,如:C:\Users\xx\pip,新建文件pip.ini,內容如下
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple