福利
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()