-傻瓜换源-文件操作-python基础-实现pip换源操作

由于python默认的pip镜像是国外镜像,下载安装难免会出点小故障,所以将镜像设置为国内镜像,不仅可以提升下载速度,还可以降低装第三方库的出错率。
pip换源是每个新手都要面临的问题,还要去搜一下怎么换,实在是太麻烦了。
这是我自己写的一个小脚本,直接运行,直接换源,傻瓜一键式换源,简化换源的操作。换源其实也就是个文件操作,非常简单,下面是代码

# -*- utf-8 -*-
# Author:MindBin
# FileName:pip_change.py
# 导入os模块 
import os
# 从time导入sleep函数
from time import sleep
configList = []
# 写入所有源信息
# 清华镜像源
configList.append("[global]\nindex-url ="
                    " https://pypi.tuna.tsinghua.edu.cn/simple/"
                       "\n[install]\ntrusted-host="
                        "pypi.tuna.tsinghua.edu.cn")
# 阿里镜像源
configList.append("[global]\nindex-url ="
                    "http://mirrors.aliyun.com/pypi/simple/"
                       "\n[install]\ntrusted-host="
                        "mirrors.aliyun.com")
# 豆瓣镜像源
configList.append("[global]\nindex-url ="
                    "http://pypi.douban.com/simple/"
                       "\n[install]\ntrusted-host="
                        "pypi.douban.com")
# 中国科学技术大学镜像源
configList.append("[global]\nindex-url ="
                    "http://pypi.mirrors.ustc.edu.cn/simple/"
                       "\n[install]\ntrusted-host="
                        "pypi.mirrors.ustc.edu.cn")
# 山东理工大学镜像源
configList.append("[global]\nindex-url ="
                    "http://pypi.sdutlinux.org/simple/"
                       "\n[install]\ntrusted-host="
                        "pypi.sdutlinux.org")
# 华中理工大学镜像源
configList.append("[global]\nindex-url ="
                    "http://pypi.hustunique.com/simple/"
                       "\n[install]\ntrusted-host="
                        "pypi.hustunique.com")
def menu():
    print("1.清华镜像源")
    print("2.阿里镜像源")
    print("3.豆瓣镜像源")
    print("4.中国科技大学镜像源")
    print("5.山东理工大学镜像源")
    print("6.华中理工大学镜像源")
    print("0.退出")
    return int(input("请输入序号:"))

if __name__ == "__main__":
    choice = menu()
    if choice == 0:
        os.system("cls")
        exit(0)
    print("正在设置镜像源中.....")
    # 由于每使用者的用户名都不同,故使用以下方法指定uer路径
    pip_path = os.environ["USERPROFILE"] + "\\pip\\"
    if not os.path.exists(pip_path):
    	# 不存在就建立
        os.makedir(pip_path)
    with open(pip_path+"pip.ini","w+") as f:
    	# 写入配置
        f.write(configList[choice - 1])
    # 更新pip
    print("正在更新pip")
    os.system("pip install pip --upgrade --user")
    print("Over thinks using this script.")
    sleep(3)
    os.system("cls")
    exit(0) 

QQ:781742062

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值