Nuitka 打包Python程序

文章目錄

  • Nuitka 打包Python程序
      • 🚀 **一、Nuitka 核心優勢**
      • ?? **二、環境準備(Windows 示例)**
      • 📦 **三、基礎打包命令**
        • **單文件腳本打包**
        • **帶第三方庫的項目**
      • 🛠? **四、高級配置選項**
        • **示例:完整命令**
      • ?? **五、常見問題與解決**
      • 📊 **六、Nuitka vs 其他工具**
      • 💎 **七、最佳實踐建議**
      • 💡 **八、使用舉例**
      • 總結

Nuitka 打包Python程序

需要把Python寫的代碼打包給朋友,于是通過DeepSeek檢索了Nuitka的使用,之前用過pyinstaller,參考我的博客PySide6 GUI 學習筆記——Python文件編譯打包,這次我采用Nuitka打包一個簡單的程序。
以下是使用 Nuitka 將 Python 程序打包為獨立 EXE 文件的完整指南,結合核心優勢、詳細步驟和避坑技巧:


🚀 一、Nuitka 核心優勢

  1. 性能卓越
    • 將 Python 編譯為 C++ 代碼,運行速度比傳統打包工具快 30%-300%。
    • 支持 SIMD 指令集優化(如 AVX2/SSE4),適合高頻計算場景(金融量化、工業視覺)。
  2. 安全加固
    • 三重防護機制:控制流扁平化、字符串常量加密(AES-256-CTR)、元數據混淆,大幅提高反編譯成本。
  3. 體積控制
    • 基礎增量僅 2-5MB,支持 UPX 壓縮(體積減少 60%)。
  4. 跨平臺支持
    • 兼容 Windows/macOS/Linux,完美支持 Python 3.4-3.13 和 Python 2.7。

?? 二、環境準備(Windows 示例)

  1. 安裝依賴
    • Python 環境:推薦 Python 3.10+(需與目標環境一致)。
    • C 編譯器:安裝 MinGW64(選擇 x86_64-posix-seh 版本),并添加 C:\mingw64\bin 到系統 PATH。
    • 驗證安裝
      pip install nuitka
      python -m nuitka --version  # 輸出版本號即成功
      

📦 三、基礎打包命令

單文件腳本打包
python -m nuitka --standalone --onefile your_script.py
  • --standalone:生成獨立 EXE(包含所有依賴)。
  • --onefile:輸出為單個 EXE 文件。
  • 輸出位置:生成 your_script.dist 目錄,內含 EXE 文件。
帶第三方庫的項目
python -m nuitka --standalone --onefile --include-package=numpy,requests app.py
  • --include-package:顯式包含動態導入的庫(如 numpyrequests)。

🛠? 四、高級配置選項

參數作用
--enable-plugin=tk-inter啟用 GUI 插件支持(如 Tkinter/PyQt)
--windows-icon=app.ico設置 EXE 圖標(需 .ico 格式)
--remove-output編譯后刪除臨時文件
--jobs=4多線程編譯加速(根據 CPU 核心數調整)
--lto=yes啟用鏈接時優化(提升運行效率)
--disable-console隱藏控制臺窗口(適用于 GUI 程序)
示例:完整命令
python -m nuitka --standalone --onefile --enable-plugin=pyqt5 --windows-icon=app.ico --jobs=4 app.py

?? 五、常見問題與解決

  1. 動態導入失敗

    • 現象importlib.import_module("mod") 未包含依賴。
    • 解決:添加 --include-package=mod--include-module=mod
  2. 殺毒軟件誤報

    • 解決:編譯時添加數字簽名,或提交至殺毒軟件白名單。
  3. 大型庫(如 PyTorch)打包失敗

    • 解決:顯式啟用插件和依賴:
      python -m nuitka --standalone --include-module=torch._C --include-package=torch-static app.py
      
  4. 編譯速度慢

    • 優化:使用 --jobs=N 多線程編譯,或跳過調試信息 --disable-console

📊 六、Nuitka vs 其他工具

特性NuitkaPyInstallercx_Freeze
執行速度????? (快 30-300%)?????
加密強度??????????
體積控制???? (支持 UPX)?????
依賴處理智能分析依賴樹需手動指定需復雜配置
適用場景商業分發/高性能計算簡單腳本高度自定義項目

💎 七、最佳實踐建議

  1. 使用虛擬環境

    • 通過 Miniconda 創建隔離環境,避免冗余依賴:
      conda create -n pack_env python=3.10
      conda activate pack_env
      pip install nuitka pandas numpy
      
  2. 分離資源文件

    • 數據文件通過 --include-data-dir=res/;res/ 打包,代碼內用 sys._MEIPASS 訪問。
  3. 敏感信息保護

    • 結合 Cython 混合編譯核心模塊(先轉 .pyx 再編譯):
      # setup_cython.py
      from distutils.core import setup
      from Cython.Build import cythonize
      setup(ext_modules=cythonize("secret_module.pyx"))
      
      再用 Nuitka 打包主程序。

💡 八、使用舉例

我有一個名為google_click.py的文件,需要打包成exe文件,使用了如下的命令:

python -m nuitka --standalone --onefile --jobs=8 --output-dir=out  --lto=
yes google_click.py

運行過程如下:

Nuitka-Options: Used command line options:
Nuitka-Options:   --standalone --onefile --jobs=8 --output-dir=out --lto=yes google_click.py
Nuitka: Starting Python compilation with:
Nuitka:   Version '2.7.10' on Python 3.12 (flavor 'Unknown') commercial grade 'not installed'.
Nuitka-Plugins:options-nanny: Using module 'trio._core._ki' (version 0.30.0) with incomplete support: Disabled careful
Nuitka-Plugins:options-nanny: handling of KeyboardInterrupt in 'trio'
Nuitka-Plugins:anti-bloat: Not including 'pandas.core._numba.extensions' automatically in order to avoid bloat, but this may
Nuitka-Plugins:anti-bloat: cause: no numba acceleration.
Nuitka: Completed Python level compilation and optimization.
Nuitka: Generating source code for C backend compiler.
Nuitka: Running data composer tool for optimal constant value handling.                                         
Nuitka: Running C compilation via Scons.
Nuitka-Scons: Backend C compiler: cl (cl 14.3).
Nuitka-Scons: Backend C linking with 723 files (no progress information available for this stage).
Nuitka-Scons: Compiled 723 C files using clcache with 0 cache hits and 723 cache misses.
Nuitka-Plugins:dll-files: Found 1 file DLLs from selenium installation.
Nuitka will make use of Dependency Walker (https://dependencywalker.com) tool
to analyze the dependencies of Python extension modules.Is it OK to download and put it in 'C:\Users\yeshe\AppData\Local\Nuitka\Nuitka\Cache\DOWNLO~1\depends\x86_64'.Fully automatic, cached. Proceed and download? [Yes]/No : yes
Nuitka: Downloading 'https://dependencywalker.com/depends22_x64.zip'.
Nuitka: Extracting to 'C:\Users\yeshe\AppData\Local\Nuitka\Nuitka\Cache\DOWNLO~1\depends\x86_64\depends.exe'
Nuitka-Plugins:data-files: Included data file 'certifi\cacert.pem' due to package data for 'certifi'.
Nuitka-Plugins:data-files: Included data file 'pandas\io\formats\templates\html.tpl' due to package data directory 'templates'
Nuitka-Plugins:data-files: for 'pandas.io.formats'.
Nuitka-Plugins:data-files: Included data file 'pandas\io\formats\templates\html_style.tpl' due to package data directory      
Nuitka-Plugins:data-files: 'templates' for 'pandas.io.formats'.
Nuitka-Plugins:data-files: Included data file 'pandas\io\formats\templates\html_table.tpl' due to package data directory      
Nuitka-Plugins:data-files: 'templates' for 'pandas.io.formats'.
Nuitka-Plugins:data-files: Included data file 'pandas\io\formats\templates\latex.tpl' due to package data directory
Nuitka-Plugins:data-files: 'templates' for 'pandas.io.formats'.
Nuitka-Plugins:data-files: Included data file 'pandas\io\formats\templates\latex_longtable.tpl' due to package data directory 
Nuitka-Plugins:data-files: 'templates' for 'pandas.io.formats'.
Nuitka-Plugins:data-files: Included data file 'pandas\io\formats\templates\latex_table.tpl' due to package data directory     
Nuitka-Plugins:data-files: 'templates' for 'pandas.io.formats'.
Nuitka-Plugins:data-files: Included data file 'pandas\io\formats\templates\string.tpl' due to package data directory
Nuitka-Plugins:data-files: 'templates' for 'pandas.io.formats'.
Nuitka-Plugins:data-files: Included 604 data files due to package data directory 'zoneinfo' for 'pytz'.
Nuitka-Plugins:data-files: Included data file 'selenium\webdriver\common\mutation-listener.js' due to package data directory  
Nuitka-Plugins:data-files: '.' for 'selenium'.
Nuitka-Plugins:data-files: Included data file 'selenium\webdriver\common\linux\selenium-manager' due to package data directory
Nuitka-Plugins:data-files: '.' for 'selenium'.
Nuitka-Plugins:data-files: Included data file 'selenium\webdriver\common\macos\selenium-manager' due to package data directory
Nuitka-Plugins:data-files: '.' for 'selenium'.
Nuitka-Plugins:data-files: Included data file 'selenium\webdriver\firefox\webdriver_prefs.json' due to package data directory 
Nuitka-Plugins:data-files: '.' for 'selenium'.
Nuitka-Plugins:data-files: Included data file 'selenium\webdriver\remote\findElements.js' due to package data directory '.'   
Nuitka-Plugins:data-files: for 'selenium'.
Nuitka-Plugins:data-files: Included data file 'selenium\webdriver\remote\getAttribute.js' due to package data directory '.'   
Nuitka-Plugins:data-files: for 'selenium'.
Nuitka-Plugins:data-files: Included data file 'selenium\webdriver\remote\isDisplayed.js' due to package data directory '.' for
Nuitka-Plugins:data-files: 'selenium'.
Nuitka-Postprocessing: Creating single file from dist folder, this may take a while.
Nuitka-Onefile: Running bootstrap binary compilation via Scons.
Nuitka-Scons: Onefile C compiler: cl (cl 14.3).
Nuitka-Scons: Onefile C linking.
Nuitka-Scons: Compiled 1 C files using clcache with 0 cache hits and 1 cache misses.
Nuitka-Onefile: Using compression for onefile payload.
Nuitka-Onefile: Onefile payload compression ratio (24.35%) size 152398972 to 37107219.
Nuitka-Onefile: Keeping onefile build directory 'out\google_click.onefile-build'.
Nuitka: Keeping dist folder 'out\google_click.dist' for inspection, no need to use it.
Nuitka: Keeping build directory 'out\google_click.build'.
Nuitka: Successfully created 'D:\Source\Repos\Visual Studio Code\some_selenium_app\out\google_click.exe'.

我指定里一個out文件夾,生成的獨立運行文件放在了該文件夾中:

文件目錄

總結

Nuitka 憑借編譯級優化工業級安全,已成為 Python 打包的首選方案,尤其適合:

  • 🔒 商業軟件分發(防源碼泄露)
  • ? 高頻計算場景(實時數據處理)
  • 🔧 混合 C/C++ 項目(如深度學習框架集成)

命令模板:

python -m nuitka --standalone --onefile --enable-plugin=upx --jobs=4 --lto=yes app.py

遇到復雜依賴時,優先通過 --include-package 顯式聲明缺失模塊,并通過 Nuitka 官方文檔 查詢插件支持列表。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/86408.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/86408.shtml
英文地址,請注明出處:http://en.pswp.cn/web/86408.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

自動獲取文件的內存大小怎么設置?批量獲取文件名和內存大小到Excel中的方法

在對重要數據進行備份或遷移操作前,為確保備份全面無遺漏,且合理規劃目標存儲設備的空間,會將文件名和內存提取到 Excel。比如,某個部門要將舊電腦中的文件遷移到新服務器,提前整理文件信息,能清晰知道所需…

創建型設計模式——單例模式

單例設計模式 什么是創建型設計模式有哪些創建型設計模式 單例設計模式實現方法餓漢式單例懶漢式單例實現方法 CSDN——C單例模式詳解 單例設計模式是一種創建型設計模式 什么是創建型設計模式 創建型設計模式,就是通過控制對象的創建方式來解決設計問題。 有哪…

html 照片環 - 圖片的動態3D環繞

html 照片環 - 圖片的動態3D環繞 引言一、源碼二、圖轉base64參考鏈接 引言 效果展示&#xff1a; 一、源碼 原始圖片的base64編碼字符太多了&#xff0c;博客放不下&#xff0c;將圖片縮小后的加入html的源碼如下&#xff1a; <!DOCTYPE html> <html><hea…

ADIOS2 介紹與使用指南

文章目錄 ADIOS2 介紹與使用指南什么是ADIOS2?ADIOS2 的主要特點ADIOS2 核心概念ADIOS2 安裝Linux 系統安裝Windows 安裝 ADIOS2 基本使用C 示例Python 示例 ADIOS2 高級特性并行I/O流模式 ADIOS2 引擎類型性能優化建議總結 ADIOS2 介紹與使用指南 什么是ADIOS2? ADIOS2(Ad…

網絡安全 vs 信息安全的本質解析:數據盾牌與網絡防線的辯證關系關系

在數字化生存的今天&#xff0c;每一次手機支付、每一份云端文檔、每一條醫療記錄的背后&#xff0c;都矗立著這兩座安全堡壘。理解它們的協同邏輯&#xff0c;不僅是技術從業者的必修課&#xff0c;更是企業構建數字防護體系的底層認知 —— 畢竟當勒索軟件同時切斷 "護城…

ping-pong操作

常見不匹配的原因 瞬時數據率的差異&#xff1b; 數據順序的差異&#xff1b; 對比維度PipelineFIFOPing-Pong邏輯復制結構類型時序分級推進&#xff08;寄存器鏈&#xff09;環形隊列&#xff08;緩沖區&#xff09;雙緩沖區&#xff08;輪換使用&#xff09;功能塊并行&am…

21.合并兩個有序鏈表

將兩個升序鏈表合并為一個新的 升序 鏈表并返回。新鏈表是通過拼接給定的兩個鏈表的所有節點組成的。 思路&#xff1a;這里使用的主要數據結構是單鏈表。該算法采用經典的雙指針技術來合并列表。 A dummy node is created; this node does not hold any meaningful value b…

vue3中簡單易懂說明nextTick的使用

nextTick(): 等待下一次 DOM 更新刷新的工具方法 重點解釋: 當你在 Vue 中更改響應式狀態時&#xff0c;最終的 DOM 更新并不是同步生效的&#xff0c;而是由 Vue 將它們緩存在一個隊列中&#xff0c;直到下一個“tick”才一起執行。這樣是為了確保每個組件無論發生多少狀態改變…

gRPC 相關介紹

介紹 依賴兩大技術 HTTP/2 作為傳輸協議 gRPC 底層用 HTTP/2&#xff0c;它支持&#xff1a; 多路復用&#xff08;在一條 TCP 連接中并行傳輸多個請求和響應&#xff09;二進制傳輸&#xff08;更緊湊、高效&#xff09;流式傳輸&#xff08;客戶端流、服務端流、雙向流&…

PyTorch 模型鏡像下載與安裝指南

在國內&#xff0c;由于網絡限制&#xff0c;直接從 PyTorch 官方源下載可能會遇到速度慢或無法訪問的問題。為了解決這一問題&#xff0c;可以使用國內鏡像源來加速下載和安裝 PyTorch。 文章目錄 安裝指定版本的 PyTorch&#xff08;以 CUDA 11.8 為例&#xff09;安裝 CPU 版…

2025年SVN學習價值分析

?? 一、SVN的現狀與應用場景分析 仍在特定領域發揮作用 傳統企業維護場景&#xff1a;在金融、電信、政府等采用集中式開發流程的機構中&#xff0c;許多遺留系統仍使用SVN管理。這些系統往往體量龐大、架構穩定&#xff0c;遷移成本高&#xff0c;因此SVN短期內不會被完全替…

JavaScript中的10種排序算法:從入門到精通

作為前端開發者&#xff0c;排序算法是我們必須掌握的基礎知識。無論是在面試中&#xff0c;還是在實際開發中處理數據展示時&#xff0c;排序都是一個常見需求。今天&#xff0c;我將用通俗易懂的方式&#xff0c;帶你了解JavaScript中最常見的10種排序算法。 1. 冒泡排序 - …

【微信小程序】6、SpringBoot整合WxJava獲取用戶手機號

1、手機號快速驗證組件 手機號快速驗證組件 旨在幫助開發者向用戶發起手機號申請&#xff0c;并且必須經過用戶同意后&#xff0c;開發者才可獲得由平臺驗證后的手機號&#xff0c;進而為用戶提供相應服務。 該能力與手機號實時驗證組件的區別為&#xff1a; 手機號快速驗證…

redis8.0新特性:原生JSON支持詳解

文章目錄 一、寫在前面二、使用1、基本命令&#xff08;1&#xff09;JSON.SET 設置 JSON 值&#xff08;2&#xff09;JSON.GET 獲取 JSON 值&#xff08;3&#xff09;JSON.DEL 刪除 JSON 值&#xff08;4&#xff09;JSON.MGET 批量獲取&#xff08;5&#xff09;JSON.MSET …

QT網絡調試助手開發全指南,軟件設計圖預研,后續文檔跟進補充

網絡調試助手 1 TCP網絡調試助手 1.1 項目概述 網絡相關的一些基礎概念學習QTcpServer 學習QTcpClient 學習TextEdit特定位置輸入文字顏色學習網絡通信相關知識點 復習鞏固之前UI控件 程序運行如下圖所示 1.2 開發流程 1.3 QTtcp 服務器的關鍵流程 工程建立&#xff0c;需要在…

網絡分層模型與協議體系技術研究報告

網絡分層模型是計算機網絡體系結構的核心框架&#xff0c;它通過將復雜的網絡通信過程分解為多個層次&#xff0c;使網絡設計、實現和維護變得更加模塊化和標準化。 一、分層模型概念 1、OSI七層模型的詳細解析 開放系統互連參考模型&#xff08;OSI/RM&#xff09;是國際標…

C++面向對象7——C繼承與C++繼承對比、C++繼承詳解

繼承 C語言與C繼承機制的對比與實現 一、C語言模擬繼承的實現方法 C語言不支持面向對象編程的原生繼承機制&#xff0c;但可以通過結構體嵌套和函數指針組合來模擬。 1. 結構體嵌套實現"is-a"關系 // 基類&#xff1a;Shape typedef struct {int x;int y; } Sha…

運維打鐵: Windows 服務器基礎運維要點解析

文章目錄 思維導圖一級節點&#xff1a;Windows 服務器基礎運維要點 詳細內容解析系統安裝與配置硬件準備安裝介質選擇系統安裝過程初始配置 日常監控與維護性能監控服務狀態檢查日志管理 安全管理賬戶與權限管理防火墻配置病毒防護 備份與恢復備份策略制定備份工具使用恢復測試…

Python實例題:基于量子計算的優化算法實現(量子計算、優化理論)

目錄 Python實例題 題目 問題描述 解題思路 關鍵代碼框架 難點分析 擴展方向 Python實例題 題目 基于量子計算的優化算法實現&#xff08;量子計算、優化理論&#xff09; 問題描述 開發一個基于量子計算的優化算法實現&#xff0c;包含以下功能&#xff1a; 量子計…

基本算法--藍橋杯備考

1.前綴和 1.定義 假設有一個數組a[n],要計算它的前j個元素的和為 a[0]a[1]...a[j-1] 時間復雜度為O(j)&#xff0c;且隨著j的變大時間復雜度越來越大。 使用了前綴和算法則為 sum[j]-sum[j-1] 時間復雜度是O(1)&#xff0c;且數據越大優勢越明顯。 2.例題一 詳解見《可…