鱼C论坛

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

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

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

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

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

x
pip太慢?试试以下这个切换镜像源的小工具

  1. import subprocess
  2. import tkinter as tk
  3. import tkinter.ttk as ttk

  4. import requests

  5. def get_trusted_host(url):
  6.     try:
  7.         response = requests.get(url)
  8.     except requests.exceptions.SSLError as e:
  9.         host = e.request.url.split("/")[2]
  10.         return host
  11.     except Exception as e:
  12.         pass

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

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

  29. class PipMirrorChanger(ttk.Frame):
  30.     def __init__(self, master=None):
  31.         super().__init__(master)
  32.         self.master = master
  33.         self.master.title("更改pip镜像源")
  34.         self.master.resizable(False, False)
  35.         self.create_widgets()
  36.         self.grid()

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

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

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

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

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

  52.         self.custom_entry_var = tk.StringVar()

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

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

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

  61.     def change_mirror(self):
  62.         pip_name = self.pip_entry.get().strip()
  63.         mirror_name = self.mirror_combobox.get()
  64.         if mirror_name == "自定义":
  65.             mirror_url = self.custom_entry.get().strip()
  66.             trust_host = get_trusted_host(mirror_url)
  67.         else:
  68.             mirror_url = MIRROR_DICT[mirror_name]
  69.             trust_host = TRUSTED_HOST_DICT[mirror_name]
  70.         command = f"{pip_name} config set global.index-url {mirror_url}"
  71.         command2 = f"{pip_name} config set install.trusted-host {trust_host}"
  72.         result = subprocess.run(
  73.             command, shell=True, capture_output=True, encoding="utf-8"
  74.         )
  75.         subprocess.run(command2, shell=True, capture_output=True, encoding="utf-8")
  76.         test = subprocess.run(
  77.             f"{pip_name} config list -v",
  78.             shell=True,
  79.             capture_output=True,
  80.             encoding="utf-8",
  81.         )
  82.         self.result_text.delete(1.0, tk.END)
  83.         if result.returncode == 0:
  84.             self.result_text.insert(
  85.                 tk.END,
  86.                 f"更改镜像源成功!\n您选择的镜像源是:{mirror_name}\n您使用的镜像源地址是:{mirror_url}\ndebugging test:\n{test}\n",
  87.             )
  88.         else:
  89.             self.result_text.insert(
  90.                 tk.END,
  91.                 f"更改镜像源失败!\n请检查您输入的pip命令名称和镜像源地址是否正确。\n错误信息如下:\n{result.stderr}\ndebugging test\n{test}\n",
  92.             )

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

  100. def main():
  101.     root = tk.Tk()
  102.     app = PipMirrorChanger(master=root)
  103.     app.mainloop()

  104. if __name__ == "__main__":
  105.     main()
复制代码


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


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

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

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

使用道具 举报

发表于 2023-10-2 15:11:32 | 显示全部楼层
具体怎么用能说说吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

你可以看看,程序有说明
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

使用道具 举报

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

苹果?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

Linux manjaro!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

pip没用,Tkinter还出错

,算了——毕竟从始至终我都没主动下python
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-14 22:44:18 | 显示全部楼层
我猜,转载至gpt?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 00:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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