企业网站后台模板,需求不明确的软件开发模型,产品网站建设必要性,浙江城建建设集团网站最近在学习python#xff0c;由于不同的版本之间的差距较大#xff0c;如果是用环境变量来配置python的话#xff0c;会需要来回改#xff0c;于是请教得知可以用conda来管理#xff0c;但是conda在管理的时候老是要输入命令#xff0c;感觉也很烦#xff0c;于是让gpt帮…最近在学习python由于不同的版本之间的差距较大如果是用环境变量来配置python的话会需要来回改于是请教得知可以用conda来管理但是conda在管理的时候老是要输入命令感觉也很烦于是让gpt帮写了一个很简陋的conda环境管理界面纯属个人学习不喜勿喷下面是最终的效果 1、左侧的激活按钮没什么用
2、中间的输入框是要安装的包名输入完成之后点击安装即可把包安装到对应的环境比如我在ai312对应的输入框中输入numpy那么就是在a312这个env下面安装numpy的包其实背后执行的命令就是:conda install numpy -n ai312 -y
3、下面的两个输入框第一个是环境名称比如ai312第二个是python的版本比如3.8 直接上代码
import os
import subprocess
import json
import re
import tkinter as tk
from tkinter import messagebox, simpledialog
from tkinter import scrolledtext
import threadingclass CondaEnvManager:def __init__(self, root):self.root rootself.root.title(Conda 环境管理器)self.root.geometry(600x400)# 获取 Conda 路径self.conda_path self.get_conda_path()self.env_list_frame tk.Frame(self.root)self.env_list_frame.pack(pady10, filltk.BOTH, expandTrue)self.create_env_frame tk.Frame(self.root)self.create_env_frame.pack(pady10)self.create_env_label tk.Label(self.create_env_frame, text创建新环境:)self.create_env_label.grid(row0, column0, padx5)self.new_env_name tk.Entry(self.create_env_frame)self.new_env_name.grid(row0, column1, padx5)# 新增 Python 版本输入框self.python_version_label tk.Label(self.create_env_frame, textPython 版本:)self.python_version_label.grid(row1, column0, padx5)self.python_version_entry tk.Entry(self.create_env_frame)self.python_version_entry.grid(row1, column1, padx5)self.create_env_button tk.Button(self.create_env_frame, text创建, commandself.create_env)self.create_env_button.grid(row0, column2, padx5)self.refresh_envs()# 日志输出框self.log_output scrolledtext.ScrolledText(self.root, width70, height15)self.log_output.pack(pady10, filltk.BOTH, expandTrue)def get_conda_path(self):# 提示用户输入 Conda 路径while True:conda_path simpledialog.askstring(Conda 路径, 请输入 conda.exe 的完整路径:)if not conda_path:messagebox.showerror(错误, Conda 路径不能为空请重试。)continue# 检查路径是否有效if os.path.isfile(conda_path) and conda in conda_path:return conda_pathelse:messagebox.showerror(错误, f路径 {conda_path} 无效请重试。)def refresh_envs(self):for widget in self.env_list_frame.winfo_children():widget.destroy()try:# 使用用户输入的 Conda 路径result subprocess.run([self.conda_path, env, list, --json], capture_outputTrue, textTrue)output result.stdout# 提取 JSON 数据try:json_match re.search(r\{.*\}, output, re.DOTALL)if not json_match:raise ValueError(No valid JSON data found in the output.)json_data json_match.group(0)data json.loads(json_data)except json.JSONDecodeError as e:messagebox.showerror(错误, f解析 JSON 数据失败: {e})returnexcept ValueError as e:messagebox.showerror(错误, f提取 JSON 数据失败: {e})returnenvs data.get(envs, [])if not envs:messagebox.showinfo(信息, 未找到任何环境。)returnfor env in envs:env_name os.path.basename(env)env_frame tk.Frame(self.env_list_frame)env_frame.pack(pady5, filltk.X)# 激活按钮放到左边activate_button tk.Button(env_frame, text激活, commandlambda eenv_name: self.activate_env(e))activate_button.pack(sidetk.LEFT, padx5)# 环境名称标签env_label tk.Label(env_frame, textenv_name, anchorw, width20)env_label.pack(sidetk.LEFT, padx5)# 输入框居中并拉长package_entry tk.Entry(env_frame, width30)package_entry.pack(sidetk.LEFT, padx5, expandTrue, filltk.X)# 安装按钮放到右边install_button tk.Button(env_frame, text安装, commandlambda eenv_name, ppackage_entry: self.start_installation(e, p.get()))install_button.pack(sidetk.RIGHT, padx5)except Exception as e:messagebox.showerror(错误, f获取环境失败: {e})def start_installation(self, env_name, package_name):if not package_name:messagebox.showwarning(警告, 请输入包名。)returnself.log_output.delete(1.0, tk.END)threading.Thread(targetself.install_package, args(env_name, package_name)).start()def install_package(self, env_name, package_name):try:process subprocess.Popen([self.conda_path, install, package_name, -n, env_name, -y],stdoutsubprocess.PIPE,stderrsubprocess.STDOUT,textTrue)for line in process.stdout:self.log_output.insert(tk.END, line)self.log_output.see(tk.END)process.wait()messagebox.showinfo(成功, f在 {env_name} 中安装了 {package_name}。)except subprocess.CalledProcessError as e:messagebox.showerror(错误, f安装包失败: {e})def activate_env(self, env_name):subprocess.run([self.conda_path, activate, env_name], checkTrue)messagebox.showinfo(激活, f激活环境: {env_name})def create_env(self):env_name self.new_env_name.get().strip()python_version self.python_version_entry.get().strip() # 获取输入的 Python 版本if not env_name:messagebox.showwarning(警告, 请输入新环境的名称。)returnif not python_version:messagebox.showwarning(警告, 请输入 Python 版本。)returntry:subprocess.run([self.conda_path, create, --name, env_name, fpython{python_version}, -y], checkTrue)messagebox.showinfo(成功, f创建新环境: {env_name}Python 版本: {python_version}。)self.new_env_name.delete(0, tk.END)self.python_version_entry.delete(0, tk.END) # 清空 Python 版本输入框self.refresh_envs()except subprocess.CalledProcessError as e:messagebox.showerror(错误, f创建环境失败: {e})if __name__ __main__:root tk.Tk()app CondaEnvManager(root)root.mainloop()
最后通过执行pyinstaller --onefile --windowed Conda.py打包成可执行的exe文件即可
启动的时候需要输入conda所在目录本来我是想通过环境变量来设置但是代码里面读取不到于是就采取这种本方法了比如我的conda安装在: 那么启动时输入 D:/install/anaconda3/condabin/conda.bat
PS需要先安装 pyinstaller和anaconda(或者miniconda)pyinstaller可以用pip安装pip install pyinstallerconda的安装就不在这里说了跟普通的软件安装一样一直下一步即可 然后就是如何使用已经创建好的环境了此处以pycharm为例比如我创建了一个ai312的环境那么在conda的安装目录的envs目录下面就会生成一个ai312的目录 在pycharm里面选择这个python.exe文件