本文主要分享一个自己写的pip一键切换国内镜像源python脚本
import subprocess
# pip 国内镜像源加速
source_urls = [
{"name": "默认镜像源", "url": ""},
{"name": "清华大学镜像源(推荐使用)", "url": "https://pypi.tuna.tsinghua.edu.cn/simple"},
{"name": "阿里云镜像源(推荐使用)", "url": "https://mirrors.aliyun.com/pypi/simple/"},
{"name": "中国科学技术大学镜像源", "url": "https://pypi.mirrors.ustc.edu.cn/simple/"},
{"name": "豆瓣镜像源", "url": "https://pypi.douban.com/simple/"},
{"name": "网易镜像源", "url": "https://mirrors.163.com/pypi/simple/"},
{"name": "百度云镜像源", "url": "https://mirror.baidu.com/pypi/simple/"},
{"name": "华为云镜像源", "url": "https://mirrors.huaweicloud.com/repository/pypi/simple/"},
{"name": "腾讯云镜像源", "url": "https://mirrors.cloud.tencent.com/pypi/simple/"},
]
def set_pip_source(source_url):
subprocess.run(["pip", "config", "set", "global.index-url", source_url])
print(f"已设置 pip 镜像源为: {source_url}")
def reset_pip_source():
subprocess.run(["pip", "config", "unset", "global.index-url"])
print("已还原 pip 默认源")
if __name__ == '__main__':
for index, source in enumerate(source_urls):
print(f"{index}、{source['name']}")
n = int(input("请选择你要切换的镜像源,(输入数字):"))
if n == 0:
reset_pip_source()
elif 1 <= n <= 8:
set_pip_source(source_urls[n]['url'])
else:
print("输入有误!")
运行效果如下: