吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 154|回复: 2
上一主题 下一主题
收起左侧

[Python 原创] 一个用python写的pip源切换小工具,有需要的可以看看

[复制链接]
跳转到指定楼层
楼主
蜉蝣 发表于 2026-6-12 09:33 回帖奖励
为了方便切换pip源地址,用豆包写了一个小工具,试用了一下还不错,分享给有需要的朋友,源码如下:

[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import messagebox
import subprocess

# 源地址配置
SOURCES = {
    "清华源": (
        "https://pypi.tuna.tsinghua.edu.cn/simple",
        "pypi.tuna.tsinghua.edu.cn"
    ),
    "阿里云": (
        "https://mirrors.aliyun.com/pypi/simple",
        "mirrors.aliyun.com"
    ),
    "中科大": (
        "https://pypi.mirrors.ustc.edu.cn/simple",
        "pypi.mirrors.ustc.edu.cn"
    ),
    "豆瓣源": (
        "http://pypi.douban.com/simple",
        "pypi.douban.com"
    ),
    "恢复官方源": ("", "")
}

def run_cmd(cmd):
    """执行cmd命令"""
    try:
        subprocess.check_call(cmd, shell=True)
        return True
    except Exception as e:
        return False

def switch_source(name):
    """切换源"""
    url, host = SOURCES[name]
    if name == "恢复官方源":
        # 清空配置
        ok1 = run_cmd("pip config unset global.index-url")
        ok2 = run_cmd("pip config unset install.trusted-host")
        if ok1 and ok2:
            messagebox.showinfo("成功", "已恢复为官方源!")
        else:
            messagebox.showerror("失败", "恢复失败,请以管理员身份运行程序")
    else:
        # 设置国内源
        cmd1 = f'pip config set global.index-url {url}'
        cmd2 = f'pip config set install.trusted-host {host}'
        ok1 = run_cmd(cmd1)
        ok2 = run_cmd(cmd2)
        if ok1 and ok2:
            messagebox.showinfo("成功", f"已切换至【{name}】")
        else:
            messagebox.showerror("失败", "切换失败,请以管理员身份运行程序")

def check_current():
    """查看当前源"""
    try:
        res = subprocess.check_output("pip config list", shell=True, encoding="utf-8")
        messagebox.showinfo("当前pip配置", res)
    except:
        messagebox.showerror("错误", "读取配置失败")

# 主界面
root = tk.Tk()
root.title("PIP 一键换源工具")
root.geometry("640x480")
root.resizable(False, False)

# 标题
tk.Label(root, text="Python PIP 国内源切换工具", font=("微软雅黑", 14)).pack(pady=15)

# 按钮
btn_style = {"width": 20, "height": 2, "font": ("微软雅黑", 10)}
tk.Button(root, text="切换 清华源", **btn_style, command=lambda: switch_source("清华源")).pack(pady=4)
tk.Button(root, text="切换 阿里云", **btn_style, command=lambda: switch_source("阿里云")).pack(pady=4)
tk.Button(root, text="切换 中科大", **btn_style, command=lambda: switch_source("中科大")).pack(pady=4)
tk.Button(root, text="切换 豆瓣源", **btn_style, command=lambda: switch_source("豆瓣源")).pack(pady=4)
tk.Button(root, text="恢复 官方源", **btn_style, command=lambda: switch_source("恢复官方源")).pack(pady=4)
tk.Button(root, text="查看当前配置", **btn_style, command=check_current).pack(pady=8)

root.mainloop()


免费评分

参与人数 2吾爱币 +1 热心值 +2 收起 理由
wj21 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
weidechan + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

沙发
shengforever 发表于 2026-6-12 11:07
小工具很实用,很省事
3#
ZhengXiaoJin 发表于 2026-6-12 11:18
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-6-13 04:01

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表