简化python库的安装过程,制作专属安装工具。
特点:定位一次python库的安装目录,下次安装库不再需要定位。只用输入库名称。
python库的安装方法:
1.定位到python安装目录的Scripts。例如:C:\Users\***\AppData\Local\Programs\Python\Python38\Scripts
2.将路径输入到该软件的目录路径框中
3.输入要安装的库名称。例如:要安装Requests,直接输入Requests即可。
4.点击运行,包存路径请点击保存设置。
[Python] 纯文本查看 复制代码 import os
import configparser
import tkinter as tk
from tkinter import messagebox
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("库安装器")
self.geometry("500x300")
self.config_file = "config.ini"
self.folder_path = tk.StringVar()
self.lib_name = tk.StringVar()
self.read_config()
self.create_widgets()
def read_config(self):
config = configparser.ConfigParser()
if os.path.exists(self.config_file):
config.read(self.config_file)
if "设置" in config:
self.folder_path.set(config["设置"].get("目录路径", ""))
else:
self.create_config()
def create_config(self):
config = configparser.ConfigParser()
config["设置"] = {}
config["设置"]["目录路径"] = ""
with open(self.config_file, "w") as f:
config.write(f)
def save_config(self):
config = configparser.ConfigParser()
config["设置"] = {}
config["设置"]["目录路径"] = self.folder_path.get()
with open(self.config_file, "w") as f:
config.write(f)
def create_widgets(self):
# Label and Entry for folder path
tk.Label(self, text="目录路径:").grid(row=0, column=0, padx=10, pady=10)
tk.Entry(self, textvariable=self.folder_path).grid(row=0, column=1, padx=10, pady=10)
# Label and Entry for library name
tk.Label(self, text="库名称:").grid(row=1, column=0, padx=10, pady=10)
tk.Entry(self, textvariable=self.lib_name).grid(row=1, column=1, padx=10, pady=10)
# Buttons for running and quitting
tk.Button(self, text="运行", command=self.run).grid(row=2, column=0, padx=10, pady=10)
tk.Button(self, text="退出", command=self.quit).grid(row=2, column=1, padx=10, pady=10)
# Button for saving settings
tk.Button(self, text="保存设置", command=self.save_config).grid(row=3, column=0, padx=10, pady=10)
def run(self):
folder_path = self.folder_path.get()
lib_name = self.lib_name.get()
if not os.path.exists(folder_path):
messagebox.showerror("错误", "目录路径不存在!")
else:
os.chdir(folder_path)
result = os.system(f"pip install -i https://pypi.tuna.tsinghua.edu.cn/simple {lib_name}")
if result == 0:
messagebox.showinfo("成功", "库安装成功!")
else:
messagebox.showerror("错误", f"库安装失败!错误代码: {result}")
def uninstall(self):
folder_path = self.folder_path.get()
lib_name = self.lib_name.get()
if not os.path.exists(folder_path):
messagebox.showerror("Error", "Folder path does not exist!")
else:
os.chdir(folder_path)
result = os.system(f"pip uninstall -y {lib_name}")
if result == 0:
messagebox.showinfo("Success", "Library uninstallation successful!")
else:
messagebox.showerror("Error", f"Library uninstallation failed! Error code: {result}")
app = App()
app.mainloop()
软件截图:
源码和程序下载地址:链接:https://pan.baidu.com/s/1b3cE4WuE1q561yTEIq9sfw
提取码:52pj |