經常在使用Python的時候需要安裝各種模塊,而pip是很強大的模塊安裝工具,但是由於國外官方默認pip訪問速度慢,經常被牆,導致無法安裝,所以我們最好是將自己使用的pip源更換一下,這樣就能解決被牆導致的裝不上庫的煩惱。
網上有很多可用的國內鏡像源,例如
http://pypi.douban.com/ 豆瓣
http://pypi.hustunique.com/ 華中理工大學
http://pypi.sdutlinux.org/ 山東理工大學
http://pypi.mirrors.ustc.edu.cn/ 中國科學技術大學
https://pypi.tuna.tsinghua.edu.cn/simple 清華
http://mirrors.aliyun.com/pypi/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 gevent
,這樣就會從清華這邊的鏡像去安裝gevent庫。
永久使用:
Linux下,修改 ~/.pip/pip.conf (沒有就自己創建), 修改 index-url至清華源,簡潔的配置內容如下:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
windows下,直接在user目錄中創建一個pip目錄,如:C:\Users\xx\pip,新建文件pip.ini,配置同上,稍微詳細一點的配置內容可以如下
注意:需要把漢字註釋去掉,否則pip 會報錯
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple #清華源,可以換成其他的源
trusted-host = pypi.tuna.tsinghua.edu.cn #添加清華源爲可信主機,要不然可能報錯
disable-pip-version-check = true #取消pip版本檢查,排除每次都報最新的pip
timeout = 120
配置文件可選路徑
Linux/Unix
/etc/pip.conf
~/.pip/pip.conf
~/.config/pip/pip.conf
Mac OSX
~/Library/Application Support/pip/pip.conf
~/.pip/pip.conf
/Library/Application Support/pip/pip.conf
Windows
%APPDATA%\pip\pip.ini
%HOME%\pip\pip.ini
C:\Documents and Settings\All Users\Application Data\PyPA \pip\pip.conf (Windows XP)
C:\ProgramData\PyPA\pip\pip.conf (Windows 7 and later)
升級所有第三發包
pip list --outdated | grep '^[a-z]* (' | cut -d " " -f 1 | xargs pip install -U
或者
pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs pip install –U