Python pip 换国内软件源

Python 安装成功之后默认是国外的pypi源,pip安装第三方库时总会出现这样或那样的问题. 更换一个国内的软件源是一个必要的选择

最新版PIP使用该命令:

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

1
2
3
4
5
6
7
8
9
mkdir ~/.pip
cat>~/.pip/pip.conf<<EOF
[global]
trusted-host = pypi.tuna.tsinghua.edu.cn
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

[list]
format=columns
EOF

Linux系统

  • 在主目录下创建.pip文件夹

mkdir ~/.pip

  • 在该目录下创建pip.conf文件,写入为
1
2
3
[global]
trusted-host =  pypi.douban.com
index-url = https://pypi.douban.com/simple

由于最新的pip安装需要使用的https加密,所以在此需要添加trusted-host

Windows系统

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
mkdir %HOMEPATH%\.pip

set FILE=%HOMEPATH%\.pip\pip.conf

echo [global] > %FILE%
echo trusted-host = pypi.tuna.tsinghua.edu.cn >> %FILE%
echo index-url = https://pypi.tuna.tsinghua.edu.cn/simple >> %FILE%
echo.  >> %FILE%
echo [list] >> %FILE%
echo format=columns >> %FILE%
  • %HOMEPATH%目录下创建.pip文件夹

  • 在该目录下创建pip.ini或者pip.conf文件,写入同样的内容

临时换源

pip install ModuleName -i https://pypi.douban.com/simple

适合只是临时使用或者测试的场合,仅本次会走指定的地址获取软件

国内pip镜像源列表

站点 说明
pypi.python.org 官方源
pypi.douban.com 豆瓣源,福州
pypi.hustunique.com 华中科技大学源,武汉
pypi.tuna.tsinghua.edu.cn 清华源,北京

题外话

在使用pip list出现警告 DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.

在配置文件加入以下即可

1
2
[list]
format=columns
updatedupdated2016-10-292016-10-29