鱼C论坛

 找回密码
 立即注册
查看: 951|回复: 7

[技术交流] 【转载】pip太慢?试试以下这个切换镜像源的小工具

[复制链接]
发表于 2023-10-2 10:25:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
pip太慢?试试以下这个切换镜像源的小工具
import subprocess
import tkinter as tk
import tkinter.ttk as ttk

import requests

def get_trusted_host(url):
    try:
        response = requests.get(url)
    except requests.exceptions.SSLError as e:
        host = e.request.url.split("/")[2]
        return host
    except Exception as e:
        pass

MIRROR_DICT = {
    "原生": "https://pypi.org/simple",
    "阿里云": "https://mirrors.aliyun.com/pypi/simple",
    "清华大学": "https://pypi.tuna.tsinghua.edu.cn/simple",
    "中国科大": "https://pypi.mirrors.ustc.edu.cn/simple",
    "豆瓣": "https://pypi.douban.com/simple",
    "自定义": None,
}

TRUSTED_HOST_DICT = {
    "原生": "pypi.org",
    "阿里云": "mirrors.aliyun.com",
    "清华大学": "mirrors.tsinghua.com",
    "中国科大": "pypi.mirrors.ustc.edu.cn",
    "豆瓣": "mirrors.douban.com",
    "自定义": None,
}

class PipMirrorChanger(ttk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.master.title("更改pip镜像源")
        self.master.resizable(False, False)
        self.create_widgets()
        self.grid()

    def create_widgets(self):
        self.welcome_label = ttk.Label(self, text="欢迎使用更改pip镜像源的小工具!")
        self.welcome_label.grid(row=0, column=0)

        self.pip_label = ttk.Label(self, text="请输入pip命令的名称(默认为pip):")
        self.pip_label.grid(row=1, column=0)

        self.pip_entry = ttk.Entry(self)
        self.pip_entry.grid(row=2, column=0)
        self.pip_entry.insert(0, "pip")

        self.mirror_label = ttk.Label(self, text="请选择您想要使用的镜像源:")
        self.mirror_label.grid(row=3, column=0)

        self.mirror_combobox = ttk.Combobox(self, state="readonly")
        self.mirror_combobox.grid(row=4, column=0)
        self.mirror_combobox["values"] = list(MIRROR_DICT.keys())
        self.mirror_combobox.current(0)
        self.mirror_combobox.bind("<<ComboboxSelected>>", lambda e: self.update_entry())

        self.custom_entry_var = tk.StringVar()

        self.custom_entry = ttk.Entry(self, textvariable=self.custom_entry_var)
        self.custom_entry.grid(row=5, column=0)
        self.custom_entry.insert(0, MIRROR_DICT[self.mirror_combobox.get()])
        self.custom_entry["state"] = "disabled"

        self.change_button = ttk.Button(self, text="更改镜像源", command=self.change_mirror)
        self.change_button.grid(row=6, column=0)

        self.result_text = tk.Text(self)
        self.result_text.grid(row=7, column=0, sticky="nsew")

    def change_mirror(self):
        pip_name = self.pip_entry.get().strip()
        mirror_name = self.mirror_combobox.get()
        if mirror_name == "自定义":
            mirror_url = self.custom_entry.get().strip()
            trust_host = get_trusted_host(mirror_url)
        else:
            mirror_url = MIRROR_DICT[mirror_name]
            trust_host = TRUSTED_HOST_DICT[mirror_name]
        command = f"{pip_name} config set global.index-url {mirror_url}"
        command2 = f"{pip_name} config set install.trusted-host {trust_host}"
        result = subprocess.run(
            command, shell=True, capture_output=True, encoding="utf-8"
        )
        subprocess.run(command2, shell=True, capture_output=True, encoding="utf-8")
        test = subprocess.run(
            f"{pip_name} config list -v",
            shell=True,
            capture_output=True,
            encoding="utf-8",
        )
        self.result_text.delete(1.0, tk.END)
        if result.returncode == 0:
            self.result_text.insert(
                tk.END,
                f"更改镜像源成功!\n您选择的镜像源是:{mirror_name}\n您使用的镜像源地址是:{mirror_url}\ndebugging test:\n{test}\n",
            )
        else:
            self.result_text.insert(
                tk.END,
                f"更改镜像源失败!\n请检查您输入的pip命令名称和镜像源地址是否正确。\n错误信息如下:\n{result.stderr}\ndebugging test\n{test}\n",
            )

    def update_entry(self):
        if self.mirror_combobox.get() == "自定义":
            self.custom_entry.config(state="normal")
            self.custom_entry_var.set("https://")
        else:
            self.custom_entry.config(state="disabled")
            self.custom_entry_var.set(MIRROR_DICT[self.mirror_combobox.get()])

def main():
    root = tk.Tk()
    app = PipMirrorChanger(master=root)
    app.mainloop()

if __name__ == "__main__":
    main()

【注意是转载的,不是本人写的】


它使用 tkinter 创建了一个简单的 GUI 应用程序,用于更改 pip 镜像源。用户可以选择不同的镜像源,也可以选择自定义镜像源。

这个应用程序的主要功能包括:

1. 允许用户输入 pip 命令的名称,默认为 "pip"。
2. 提供一个下拉框,让用户选择不同的镜像源,包括原生、阿里云、清华大学等。
3. 如果用户选择 "自定义" 镜像源,允许用户手动输入自定义镜像源的地址。
4. 提供一个按钮,当用户点击它时,将根据用户的选择更新 pip 的配置文件,以使用新的镜像源。
5. 在应用程序窗口中显示结果,包括更改是否成功以及使用的镜像源信息。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-2 15:11:32 | 显示全部楼层
具体怎么用能说说吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-10-2 15:15:42 | 显示全部楼层
奋斗中的鱼 发表于 2023-10-2 15:11
具体怎么用能说说吗?

你可以看看,程序有说明
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-3 17:40:43 | 显示全部楼层
What?
 python s_pip.py
Traceback (most recent call last):
  File "/home/name/fishc/python/s_pip.py", line 2, in <module>
    import tkinter as tk
  File "/usr/lib/python3.11/tkinter/__init__.py", line 38, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
    ^^^^^^^^^^^^^^^
ImportError: libtk8.6.so: cannot open shared object file: No such file or directory
我是不是装了个盗版的python?!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

 楼主| 发表于 2023-10-3 17:41:16 | 显示全部楼层
风眠 发表于 2023-10-3 17:40
What?我是不是装了个盗版的python?!

苹果?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-3 18:02:28 | 显示全部楼层

Linux manjaro!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-3 18:06:18 | 显示全部楼层

pip没用,Tkinter还出错

,算了——毕竟从始至终我都没主动下python
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-14 22:44:18 | 显示全部楼层
我猜,转载至gpt?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 10:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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