python中nuitka使用程序打包的實現(xiàn)
目前python打包成exe的工具主要有:PyInstaller Briefcase py2exe py2app Nuitka CX_Freeze等。
不同于C++代碼,可以直接編譯成可執(zhí)行的exe文件,或者js代碼在瀏覽器中就能執(zhí)行,python代碼必須通過python解釋器來運行,很多操作系統(tǒng)都沒有預(yù)裝。所以需要通過工具將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解釋器和目標(biāo)代碼的所有依賴都打包進(jìn)去,參數(shù)--onefile將所有文件打包成一個單獨運行的可執(zhí)行文件

打包完成


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

Nuitka對更復(fù)雜的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="轉(zhuǎn)大寫", command=self.convert_upper).pack(side='left', padx=5)
ttk.Button(btn_frame, text="轉(zhuǎn)小寫", 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="字?jǐn)?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)
# 結(jié)果顯示
self.result_label = ttk.Label(frame, text="結(jié)果: ")
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="文本已轉(zhuǎn)換為大寫")
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="文本已轉(zhuǎn)換為小寫")
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)計結(jié)果", f"字?jǐn)?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"結(jié)果: {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,如下圖,正常運行。

總結(jié)
不同于其他打包工具,nuitka是將python代碼,轉(zhuǎn)換成C代碼,在通過C編譯器編譯成可執(zhí)行文件。
優(yōu)勢:支持外部資源,支持windows、linux、以及macOS系統(tǒng)。打Nuitka --help可以查看所有命令參數(shù)。
到此這篇關(guān)于python中nuitka使用程序打包的實現(xiàn)的文章就介紹到這了,更多相關(guān)python nuitka程序打包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python將string轉(zhuǎn)換到float的實例方法
在本篇文章中小編給大家分享的是關(guān)于Python將string轉(zhuǎn)換到float的實例方法以及相關(guān)知識點,需要的朋友們參考下。2019-07-07
Python連接Neo4j數(shù)據(jù)庫的操作指南
Neo4j 是一個圖形數(shù)據(jù)庫,它通過節(jié)點(Nodes)和關(guān)系(Relationships)來組織和存儲數(shù)據(jù),使用 Neo4j,可以方便地表示和操作圖數(shù)據(jù),例如社交網(wǎng)絡(luò)、推薦系統(tǒng)、知識圖譜等,本文給大家介紹了Python連接Neo4j數(shù)據(jù)庫的操作指南,需要的朋友可以參考下2025-05-05
Python使用wxPython和PyMuPDF實現(xiàn)合并PDF文檔
處理大量的PDF文檔可能會變得復(fù)雜和耗時,但是,使用Python編程和一些強大的庫,可以使這個任務(wù)變得簡單而高效,下面我們就來看看Python如何使用wxPython和PyMuPDF合并PDF文檔并自動復(fù)制到剪貼板吧2023-11-11
Python實現(xiàn)點陣字體讀取與轉(zhuǎn)換的方法
今天小編就為大家分享一篇Python實現(xiàn)點陣字體讀取與轉(zhuǎn)換的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
python 文件下載之?dāng)帱c續(xù)傳的實現(xiàn)
用python進(jìn)行文件下載的時候,一旦出現(xiàn)網(wǎng)絡(luò)波動問題,導(dǎo)致文件下載到一半。如果將下載不完全的文件刪掉,那么又需要從頭開始,如果連續(xù)網(wǎng)絡(luò)波動,是不是要頭禿了。本文提供斷點續(xù)傳下載工具方法,希望可以幫助到你2021-11-11
Python爬蟲自動化獲取華圖和粉筆網(wǎng)站的錯題(推薦)
這篇文章主要介紹了Python爬蟲自動化獲取華圖和粉筆網(wǎng)站的錯題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01

