Nuitka打包python项目

作者 :
免费
  • 正文
  • Python打包有pyinstaller nuitka cx_freeze 三款软件,下面是对比。

    功能/特性 PyInstaller Nuitka cx_Freeze
    打包类型 单文件或文件夹 单文件或文件夹 单文件或文件夹
    平台支持 跨平台(Windows、Linux、macOS) 跨平台(Windows、Linux、macOS) 跨平台(Windows、Linux、macOS)
    依赖管理 自动管理依赖 需要手动处理依赖 自动管理依赖
    打包速度 一般 一般 较慢
    运行速度 一般 一般 一般
    打包后文件大小 较大 较小 一般
    GUI支持 支持 部分支持 支持
    可执行文件类型 可执行二进制文件 可执行二进制文件 可执行二进制文件
    开发社区 活跃 活跃 相对较小

    而nuitka效果最好。

    Nuitka打包python项目,用了两天晚上写的打包Py工具。

    根据参数:D:\Program Files\Python310\python.exe -m nuitka --onefile --show-progress --remove-output --windows-disable-console --lto=no --assume-yes-for-downloads --output-dir=C:/Users/Administrator/Desktop --main=C:/Users/Administrator/Desktop/FontProject/pngtopdf.py --windows-icon-from-ico=C:/Users/Administrator/Desktop/FontProject/suguang.ico --enable-plugin=matplotlib,tk-inter

    编写的GUI程序,这里有三项输出目录,.ico,--enable-plugin=参数,做了判断。

    import tkinter as tk
    from tkinter import filedialog, messagebox
    import subprocess
    import os
    class GUIApp:
        def __init__(self, master):
            self.master = master
            self.master.title("Nuitka打包工具")
            # 设置窗口宽度和高度
            window_width = 600
            window_height = 630
            # 获取屏幕宽度和高度
            screen_width = master.winfo_screenwidth()
            screen_height = master.winfo_screenheight()
            # 计算窗口左上角坐标使其居中
            x_position = (screen_width - window_width) // 2
            y_position = (screen_height - window_height) // 2
            # 设置窗口大小和位置
            master.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")
            # 创建选择输出目录的按钮
            self.output_dir_button = tk.Button(master, text="选择输出目录", command=self.choose_output_dir)
            self.output_dir_button.pack()
            # 显示选择的输出目录路径
            self.output_dir_label = tk.Label(master, text="")
            self.output_dir_label.pack()
            # 创建选择.py文件的按钮
            self.main_file_button = tk.Button(master, text="选择.py文件", command=self.choose_main_file)
            self.main_file_button.pack()
            # 显示选择的.py文件路径
            self.main_file_label = tk.Label(master, text="")
            self.main_file_label.pack()
            # 创建选择.icon图标的按钮
            self.icon_file_button = tk.Button(master, text="选择.icon图标", command=self.choose_icon_file)
            self.icon_file_button.pack()
            # 显示选择的.icon图标路径
            self.icon_file_label = tk.Label(master, text="")
            self.icon_file_label.pack()
            # 创建输入框
            self.entry_label = tk.Label(master, text="请输入Nuitka --enable-plugin参数,多个用“,”号隔开,可以为空")
            self.entry_label.pack()
            # 创建输入框,并设置默认值为 "tk-inter"
            self.entry = tk.Entry(master, width=50)
            self.entry.insert(0, "tk-inter")
            self.entry.pack()
            # 创建按钮
            self.pack_button = tk.Button(master, text="编译开始", command=self.pack_and_run)
            self.pack_button.pack(pady=20)
            # 注:安装Nuitka和GCC(配置环境变量)
            self.developer_label = tk.Label(master, text="注:安装Nuitka和GCC(配置环境变量),输出目录不选择则会输出到.py同目录下")
            self.developer_label.pack(side="top", pady=5)
            # 创建 Text 组件,用于显示进度
            self.progress_text = tk.Text(master, height=20, width=80)
            self.progress_text.pack()
            # 初始化选择的文件路径
            self.output_dir = ""
            self.main_file = ""
            self.icon_file = ""
            # 在窗口底部添加 "Development by 速光网络-都百顺" 横向居中显示
            self.developer_label = tk.Label(master, text="Development by 速光网络-都百顺(抖音号:dubaishun12)")
            self.developer_label.pack(side="bottom", pady=20)
        def choose_output_dir(self):
            self.output_dir = filedialog.askdirectory(title="选择输出目录")
            self.output_dir_label.config(text=f"选择的输出目录: {self.output_dir}")
        def choose_main_file(self):
            self.main_file = filedialog.askopenfilename(title="选择.py文件")
            self.main_file_label.config(text=f"选择的.py文件: {self.main_file}")
        def choose_icon_file(self):
            self.icon_file = filedialog.askopenfilename(title="选择.icon图标")
            self.icon_file_label.config(text=f"选择的.icon图标: {self.icon_file}")
        def choose_output_dir(self):
            selected_dir = filedialog.askdirectory(title="选择输出目录")
            if selected_dir:
                self.output_dir = selected_dir
                self.output_dir_label.config(text=f"选择的输出目录: {self.output_dir}")
            else:
                self.output_dir = ""
                self.output_dir_label.config(text="选择的输出目录: 未选择")
        def choose_icon_file(self):
            selected_icon_file = filedialog.askopenfilename(title="选择.icon图标")
            if selected_icon_file:
                self.icon_file = selected_icon_file
                self.icon_file_label.config(text=f"选择的.icon图标: {self.icon_file}")
            else:
                self.icon_file = ""
                self.icon_file_label.config(text="选择的.icon图标: 未选择")
        def pack_and_run(self):
            # 获取输入的 Nuitka 参数
            nuitka_params = self.entry.get()
            # 检查是否选择了输出目录,如果没有,使用 .py 文件的目录
            if not self.output_dir:
                self.output_dir = os.path.dirname(self.main_file)
            # 构建命令列表
            command = [
                "python", "-m", "nuitka",
                "--onefile", "--show-progress", "--remove-output", "--windows-disable-console",
                f"--output-dir={self.output_dir}", f"--main={self.main_file}",
            ]
            # 如果选择了 .icon 文件,则添加相应参数
            if self.icon_file:
                command.append(f"--windows-icon-from-ico={self.icon_file}")
            # 如果手工输入了 --enable-plugin 参数,则添加相应参数
            if nuitka_params:
                command.append(f"--enable-plugin={nuitka_params}")
            try:
                # 使用 subprocess.Popen 启动 CMD 窗口并捕获输出
                process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
                # 读取并显示输出
                while True:
                    output = process.stdout.readline()
                    if not output and process.poll() is not None:
                        break
                    self.progress_text.insert(tk.END, output)
                    self.progress_text.see(tk.END)
                    self.progress_text.update_idletasks()
                # 检查进程是否成功完成
                if process.returncode == 0:
                    # 如果进程成功完成,显示成功消息
                    messagebox.showinfo("Success", "编译成功.")
                else:
                    # 如果进程失败,显示错误消息
                    messagebox.showerror("Error", f"发生错误: {process.returncode}")
            except Exception as e:
                messagebox.showerror("Error", f"发生错误: {str(e)}")
    if __name__ == "__main__":
        root = tk.Tk()
        app = GUIApp(root)
        root.mainloop()

    Nuitka打包python项目-完美源码

    Nuitka打包python项目-完美源码

    END
    如本资源侵犯了您的权益,请联系投诉邮箱admin@wmphp.com进行举报!我们将在收到邮件的1个小时内处理完毕。 本站仅为平台,发布的资源均为用户投稿或转载!所有资源仅供参考学习使用,请在下载后的24小时内删除,禁止商用! Wmphp.com(完美源码)助力正版,如您有自己的原创产品,可以联系客服投稿,代理出售! Wmphp.com(完美源码)客服QQ:136882447 Wmphp.com(完美源码)商务电话(仅对企业客户/个人用户):15120086569 (微信同步) 请注意:本站不提供任何免费的技术咨询服务,为了节约时间,下载前 请确认自己会技术
    完美源码 » Nuitka打包python项目
    3498+

    本站勉强运行

    3675+

    用户总数

    690+

    资源总数

    0+

    今日更新

    2024-4-8

    最后更新时间