python中nuitka使用程序打包的實現(xiàn)
目前python打包成exe的工具主要有:PyInstaller Briefcase py2exe py2app Nuitka CX_Freeze等。
不同于C++代碼,可以直接編譯成可執(zhí)行的exe文件,或者js代碼在瀏覽器中就能執(zhí)行,python代碼必須通過python解釋器來運行,很多操作系統(tǒng)都沒有預裝。所以需要通過工具將python代碼打包成可獨立運行的exe文件,工具主要包括PyTnstaller Briefcase py2exe py2app Nuitka CX_Freeze等,本期介紹打包工具Nuitka。
1.Pip install nuitka安裝

2.nuitka yanhua.py打包

3.執(zhí)行exe程序
發(fā)現(xiàn)直接打包好的exe文件仍然不可以在沒有安裝python的電腦上執(zhí)行

4.添加參數(shù)打包
所以我們還得再打包時加上參數(shù) --standalone,這樣才會將python解釋器和目標代碼的所有依賴都打包進去,參數(shù)--onefile將所有文件打包成一個單獨運行的可執(zhí)行文件

打包完成


一般默認的cl編譯,也可以添加mingw編譯。
打包可能出現(xiàn)以下情況,可能是微信開發(fā)者工具中ws2_32.dll導致的問題,直接卸載微信開發(fā)者工具就好了。

Nuitka對更復雜的GUI程序和第三方庫支持情況
以下是一個示例代碼
import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import os
class MyApp:
def __init__(self, root):
self.root = root
self.root.title("功能演示程序")
self.root.iconbitmap("C:/Users/Administrator/Desktop/z.ico")
self.root.geometry("800x600")
# 創(chuàng)建選項卡
self.notebook = ttk.Notebook(root)
self.notebook.pack(expand=True, fill='both', padx=5, pady=5)
# 創(chuàng)建三個選項卡頁面
self.tab1 = ttk.Frame(self.notebook)
self.tab2 = ttk.Frame(self.notebook)
self.tab3 = ttk.Frame(self.notebook)
self.notebook.add(self.tab1, text='文本處理')
self.notebook.add(self.tab2, text='文件操作')
self.notebook.add(self.tab3, text='計算工具')
self.setup_text_tab()
self.setup_file_tab()
self.setup_calc_tab()
# 創(chuàng)建狀態(tài)欄
self.status_bar = tk.Label(root, text="就緒", bd=1, relief=tk.SUNKEN, anchor=tk.W)
self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
def setup_text_tab(self):
# 文本輸入?yún)^(qū)域
frame = ttk.LabelFrame(self.tab1, text="文本處理區(qū)域", padding=10)
frame.pack(fill='both', expand=True, padx=5, pady=5)
self.text_input = tk.Text(frame, height=10)
self.text_input.pack(fill='both', expand=True, pady=5)
btn_frame = ttk.Frame(frame)
btn_frame.pack(fill='x', pady=5)
ttk.Button(btn_frame, text="轉大寫", command=self.convert_upper).pack(side='left', padx=5)
ttk.Button(btn_frame, text="轉小寫", command=self.convert_lower).pack(side='left', padx=5)
ttk.Button(btn_frame, text="清空", command=self.clear_text).pack(side='left', padx=5)
ttk.Button(btn_frame, text="字數(shù)統(tǒng)計", command=self.count_words).pack(side='left', padx=5)
def setup_file_tab(self):
frame = ttk.LabelFrame(self.tab2, text="文件操作", padding=10)
frame.pack(fill='both', expand=True, padx=5, pady=5)
ttk.Button(frame, text="選擇文件", command=self.select_file).pack(pady=5)
self.file_label = ttk.Label(frame, text="未選擇文件")
self.file_label.pack(pady=5)
self.file_content = tk.Text(frame, height=10)
self.file_content.pack(fill='both', expand=True, pady=5)
ttk.Button(frame, text="保存內(nèi)容", command=self.save_file).pack(pady=5)
def setup_calc_tab(self):
frame = ttk.LabelFrame(self.tab3, text="簡單計算器", padding=10)
frame.pack(fill='both', expand=True, padx=5, pady=5)
# 輸入框
input_frame = ttk.Frame(frame)
input_frame.pack(fill='x', pady=5)
ttk.Label(input_frame, text="數(shù)字1:").pack(side='left')
self.num1 = ttk.Entry(input_frame, width=10)
self.num1.pack(side='left', padx=5)
ttk.Label(input_frame, text="數(shù)字2:").pack(side='left')
self.num2 = ttk.Entry(input_frame, width=10)
self.num2.pack(side='left', padx=5)
# 計算按鈕
btn_frame = ttk.Frame(frame)
btn_frame.pack(fill='x', pady=5)
ttk.Button(btn_frame, text="+", command=lambda: self.calculate('+')).pack(side='left', padx=5)
ttk.Button(btn_frame, text="-", command=lambda: self.calculate('-')).pack(side='left', padx=5)
ttk.Button(btn_frame, text="×", command=lambda: self.calculate('*')).pack(side='left', padx=5)
ttk.Button(btn_frame, text="÷", command=lambda: self.calculate('/')).pack(side='left', padx=5)
# 結果顯示
self.result_label = ttk.Label(frame, text="結果: ")
self.result_label.pack(pady=5)
# 文本處理功能
def convert_upper(self):
text = self.text_input.get("1.0", tk.END)
self.text_input.delete("1.0", tk.END)
self.text_input.insert("1.0", text.upper())
self.status_bar.config(text="文本已轉換為大寫")
def convert_lower(self):
text = self.text_input.get("1.0", tk.END)
self.text_input.delete("1.0", tk.END)
self.text_input.insert("1.0", text.lower())
self.status_bar.config(text="文本已轉換為小寫")
def clear_text(self):
self.text_input.delete("1.0", tk.END)
self.status_bar.config(text="文本已清空")
def count_words(self):
text = self.text_input.get("1.0", tk.END).strip()
words = len(text.split())
chars = len(text)
messagebox.showinfo("統(tǒng)計結果", f"字數(shù)統(tǒng)計:\n單詞數(shù):{words}\n字符數(shù):{chars}")
# 文件操作功能
def select_file(self):
file_path = filedialog.askopenfilename()
if file_path:
self.file_label.config(text=file_path)
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
self.file_content.delete("1.0", tk.END)
self.file_content.insert("1.0", content)
self.status_bar.config(text=f"已打開文件:{os.path.basename(file_path)}")
except Exception as e:
messagebox.showerror("錯誤", f"無法打開文件:{str(e)}")
def save_file(self):
file_path = filedialog.asksaveasfilename(defaultextension=".txt")
if file_path:
try:
content = self.file_content.get("1.0", tk.END)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
self.status_bar.config(text=f"文件已保存:{os.path.basename(file_path)}")
except Exception as e:
messagebox.showerror("錯誤", f"保存失敗:{str(e)}")
# 計算器功能
def calculate(self, operator):
try:
num1 = float(self.num1.get())
num2 = float(self.num2.get())
if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
if num2 == 0:
raise ValueError("除數(shù)不能為零")
result = num1 / num2
self.result_label.config(text=f"結果: {result:.2f}")
self.status_bar.config(text="計算完成")
except ValueError as e:
messagebox.showerror("錯誤", str(e))
except Exception as e:
messagebox.showerror("錯誤", "請輸入有效的數(shù)字")
if __name__ == '__main__':
root = tk.Tk()
app = MyApp(root)
root.mainloop()程序運行界面如下圖

打包


在test.list中可以看到打包好的依賴項,執(zhí)行exe,如下圖,正常運行。

總結
不同于其他打包工具,nuitka是將python代碼,轉換成C代碼,在通過C編譯器編譯成可執(zhí)行文件。
優(yōu)勢:支持外部資源,支持windows、linux、以及macOS系統(tǒng)。打Nuitka --help可以查看所有命令參數(shù)。
到此這篇關于python中nuitka使用程序打包的實現(xiàn)的文章就介紹到這了,更多相關python nuitka程序打包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python實現(xiàn)提取給定網(wǎng)頁內(nèi)的所有鏈接
這篇文章主要和大家分享一個實用的Python腳本,可以實現(xiàn)從給定的網(wǎng)頁中檢索所有鏈接,并將其保存為txt文件,需要的小伙伴可以收藏一下2023-05-05
Python深度學習實戰(zhàn)PyQt5基本控件使用解析
PyQt5 提供了豐富的輸入輸出控件。本文介紹通過 QtDesigner 工具欄創(chuàng)建常用的基本控件,包括各種按鈕控件、文本輸入控件和調(diào)節(jié)輸入控件2021-10-10
pytest利用request?fixture實現(xiàn)個性化測試需求詳解
這篇文章主要為大家詳細介紹了pytest如何利用request?fixture實現(xiàn)個性化測試需求,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下2023-09-09

