一些常用的 Python 包管理器(如 pip
)的镜像源。这些镜像源可以帮助你更快地下载和安装 Python 包,尤其是在国内网络环境下。
国内常用镜像源
-
阿里云
- URL:
http://mirrors.aliyun.com/pypi/simple/
- URL:
-
清华大学
- URL:
https://pypi.tuna.tsinghua.edu.cn/simple
- URL:
-
豆瓣
- URL:
http://pypi.douban.com/simple/
- URL:
-
中国科学技术大学
- URL:
https://pypi.mirrors.ustc.edu.cn/simple/
- URL:
-
华中科技大学
- URL:
http://pypi.hustunique.com/
- URL:
-
上海交通大学
- URL:
https://pypi.sjtu.edu.cn/
- URL:
-
网易
- URL:
http://mirrors.163.com/pypi/simple/
- URL:
-
BFSU(北京外国语大学)
- URL:
https://pypi.bfsu.edu.cn/simple/
- URL:
国际常用镜像源
-
官方 PyPI
- URL:
https://pypi.org/simple/
- URL:
-
CloudSmith
- URL:
https://dl.cloudsmith.io/public/python/pypi/simple/
- URL:
-
Ruby China
- URL:
https://mirrors.ruby-china.com/pypi/simple/
- URL:
使用方法
临时使用镜像源
你可以在每次安装包时直接指定镜像源:
pip install <package_name> -i <mirror_url>
例如,使用阿里云镜像源:
pip install <package_name> -i http://mirrors.aliyun.com/pypi/simple/
设置默认镜像源
你可以在 pip
的配置文件中设置默认的镜像源。
-
查找或创建配置文件:
- 路径通常是
%USERPROFILE%\pip\pip.ini
。
- 路径通常是
-
编辑配置文件:
- 打开或创建
pip.ini
文件,添加以下内容:
[global] index-url = <mirror_url>
- 打开或创建
例如,设置清华大学镜像源为默认源:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
验证配置
你可以通过运行以下命令来验证配置是否生效:
pip config list
你应该能看到类似以下的输出:
global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
切换镜像源
如果你需要在不同的镜像源之间切换,可以使用 pip config
命令来修改配置:
pip config set global.index-url <mirror_url>
例如,切换回官方源:
pip config set global.index-url https://pypi.org/simple
//切换源
pip config set global.index-url https://dl.cloudsmith.io/public/python/pypi/simple/