スポンサーリンク

NuitkaでPython3.11で作ったGUIを実行ファイル化したときの備忘録

NuitkaでPython3.11+PySide6で作ったGUIを実行ファイル化したときの備忘録についてQiitaに投稿しました。
Python3.11(venv)+PySide6の環境でNuitkaを使用して実行ファイルを生成する – Qiita

初めてQiitaに記事投稿しました(2年くらいROMってた)。
上記の記事に記載した情報をこの記事内に書く必要はないと思うので、Qiitaに記載するには本筋から外れてるけど自分用には残しておきたい情報についてこの記事内に記載します。

この記事の中の内容は2023年5月23日時点の内容です。
記事を書いてる7月現在、細かいバージョンの差異などがあるかもしれません。

Nuitka導入によるパッケージの差分

※以下の差分はNuitka導入とPySide6のアップデートを行った状態(PySide6が6.5.0未満でビルド時にワーニングが出たため)

比較用のPyInstallerをインストールしたときのパッケージの差分

以下のコマンドでインストール

> pip install pyinstaller

その他、Nuitka導入中の備忘録

以下のコマンドで実行ファイルをビルドできたが、実行したところ正常に起動せず

> nuitka --standalone app.py
Nuitka-Options:INFO: Used command line options: --standalone app.py
Nuitka:WARNING: The Python version '3.11' is only experimentally supported by Nuitka '1.5.8', but an upcoming release will change that. In the mean time use Python version '3.10' instead or newer Nuitka.
Nuitka:INFO: Starting Python compilation with Nuitka '1.5.8' on Python '3.11' commercial grade 'not installed'.
Nuitka-Plugins:WARNING: Use '--enable-plugin=pyside6' for: Standalone mode support and Qt plugins.
Nuitka-Plugins:INFO: implicit-imports: Injecting post-module load code for module 'PySide6.QtCore':
Nuitka-Plugins:INFO: implicit-imports:     According to Yaml configuration.
Nuitka-Plugins:INFO: multiprocessing: Injecting pre-module load code for module 'multiprocessing':
Nuitka-Plugins:INFO: multiprocessing:     Monkey patching "multiprocessing" load environment.
Nuitka-Plugins:INFO: multiprocessing: Injecting post-module load code for module 'multiprocessing':
Nuitka-Plugins:INFO: multiprocessing:     Monkey patching "multiprocessing" for compiled methods.
Nuitka:INFO: Completed Python level compilation and optimization.
Nuitka:INFO: Generating source code for C backend compiler.
Nuitka:INFO: Running data composer tool for optimal constant value handling.
Nuitka:INFO: Running C compilation via Scons.
Nuitka-Scons:INFO: Backend C compiler: cl (cl 14.1).
Nuitka-Scons:INFO: Backend linking program with 405 files (no progress information available).
Nuitka-Scons:INFO: Compiled 405 C files using clcache with 0 cache hits and 405 cache misses.
Nuitka-Plugins:INFO: data-files: Included data file 'certifi\cacert.pem' due to package data for 'certifi'.
Nuitka-Plugins:INFO: dll-files: Found 1 file DLLs from cv2 installation.
Nuitka-Plugins:INFO: dll-files: Found 1 file DLLs from numpy 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\@@@@@@@@@\AppData\Local\Nuitka\Nuitka\Cache\downloads\depends\x86_64'.

Fully automatic, cached. Proceed and download? [Yes]/No : Yes
Nuitka:INFO: Downloading 'https://dependencywalker.com/depends22_x64.zip'.
Nuitka:INFO: Extracting to 'C:\Users\@@@@@@@@@\AppData\Local\Nuitka\Nuitka\Cache\downloads\depends\x86_64\depends.exe'
Nuitka:INFO: Keeping build directory 'app.build'.                             
Nuitka:INFO: Successfully created 'app.dist\app.exe'.

具体的には起動初期のログは出るものの直後にログを吐いて停止してしまう
※「処理を開始しました.」はアプリケーションの先頭でlogzeroを使用して出力している固有のログ

[I 230523 11:17:27 app:961] 処理を開始しました.
qt.qpa.plugin: Could not find the Qt platform plugin "windows" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

その後、Nuitkaのプラグイン一覧を確認
アプリケーション内でPySide6を使っている場合に専用のオプションを指定する必要があることを確認

> nuitka --plugin-list
                 The following plugins are available in Nuitka
--------------------------------------------------------------------------------
 anti-bloat        Patch stupid imports out of widely used library modules source codes.
 data-files        Include data files specified by package configuration files.
 delvewheel        Required for 'support' of delvewheel using packages in standalone mode.
 dill-compat       Required for 'dill' package compatibility.
 dll-files         Include DLLs as per package configuration files.
 enum-compat       Required for Python2 and 'enum' package.
 eventlet          Support for including 'eventlet' dependencies and its need for 'dns' package monkey patching.
 gevent            Required by the 'gevent' package.
 gi                Support for GI package typelib dependency.
 glfw              Required for OpenGL and 'glfw' package in standalone mode.
 implicit-imports  Provide implicit imports of package as per package configuration files.
 kivy              Required by 'kivy' package.
 matplotlib        Required for 'matplotlib' module.
 multiprocessing   Required by Python's 'multiprocessing' module.
 no-qt             Disable all Qt bindings for standalone mode.
 options-nanny     Inform the user about potential problems as per package configuration files.
 pbr-compat        Required by the 'pbr' package in standalone mode.
 pkg-resources     Workarounds for 'pkg_resources'.
 pmw-freezer       Required by the 'Pmw' package.
 pylint-warnings   Support PyLint / PyDev linting source markers.
 pyqt5             Required by the PyQt5 package.
 pyqt6             Required by the PyQt6 package for standalone mode.
 pyside2           Required by the PySide2 package.
 pyside6           Required by the PySide6 package for standalone mode.
 pywebview         Required by the 'webview' package (pywebview on PyPI).
 tk-inter          Required by Python's Tk modules.
 trio              Required for 'trio' package.
 upx               Compress created binaries with UPX automatically.

以下の記事なども参考にコマンドを修正して再ビルド
NuitkaでPythonプログラムを配布してみよう | インフォメーション・ディベロプメント

> nuitka --enable-plugin=pyside6 --standalone --onefile --follow-imports app.py
Nuitka-Options:INFO: Used command line options: --enable-plugin=pyside6 --standalone --onefile --follow-imports app.py
Nuitka:WARNING: The Python version '3.11' is only experimentally supported by Nuitka '1.5.8', but an upcoming release will change that. In the mean time use Python version '3.10' instead or newer Nuitka.
Nuitka-Options:INFO: Following all imports is the default for onefile mode and need not be specified.
Nuitka-Plugins:WARNING: pyside6: Make sure to use PySide 6.5.0 or higher, otherwise Qt slots won't work in all cases.
Nuitka:INFO: Starting Python compilation with Nuitka '1.5.8' on Python '3.11' commercial grade 'not installed'.
Nuitka-Plugins:INFO: pyside6: Injecting pre-module load code for module 'PySide6':
Nuitka-Plugins:INFO: pyside6:     Adding binary folder to runtime 'PATH' environment variable for proper Qt loading.
Nuitka-Plugins:INFO: implicit-imports: Injecting post-module load code for module 'PySide6.QtCore':
Nuitka-Plugins:INFO: implicit-imports:     According to Yaml configuration.
Nuitka-Plugins:INFO: pyside6: Injecting post-module load code for module 'PySide6.QtCore':
Nuitka-Plugins:INFO: pyside6:     Setting Qt library path to distribution folder. We need to avoid loading target
Nuitka-Plugins:INFO: pyside6:     system Qt plugins, which may be from another Qt version.
Nuitka-Plugins:INFO: options-nanny: Note, when using 'PySide6', consider using '--disable-console' option. Otherwise a terminal window will open. However for debugging, terminal output is the easiest way to see informative traceback information.
Nuitka-Plugins:INFO: multiprocessing: Injecting pre-module load code for module 'multiprocessing':
Nuitka-Plugins:INFO: multiprocessing:     Monkey patching "multiprocessing" load environment.
Nuitka-Plugins:INFO: multiprocessing: Injecting post-module load code for module 'multiprocessing':
Nuitka-Plugins:INFO: multiprocessing:     Monkey patching "multiprocessing" for compiled methods.
Nuitka-Plugins:WARNING: pyside6: Unwanted import of 'tkinter' that is redundant with 'PySide6' encountered. Use '--nofollow-import-to=tkinter' or uninstall it for best compatibility with pure Python execution.
Nuitka:INFO: Completed Python level compilation and optimization.
Nuitka:INFO: Generating source code for C backend compiler.
Nuitka:INFO: Running data composer tool for optimal constant value handling.                                 
Nuitka:INFO: Running C compilation via Scons.
Nuitka-Scons:INFO: Backend C compiler: cl (cl 14.1).
Nuitka-Scons:INFO: Backend linking program with 406 files (no progress information available).
Nuitka-Scons:INFO: Compiled 406 C files using clcache with 400 cache hits and 6 cache misses.
Nuitka-Plugins:INFO: data-files: Included data file 'certifi\cacert.pem' due to package data for 'certifi'.
Nuitka-Plugins:INFO: pyside6: Including Qt plugins 'iconengines,imageformats,platforms,styles,tls' below 'PySide6\qt-plugins'.
Nuitka-Plugins:INFO: dll-files: Found 1 file DLLs from cv2 installation.
Nuitka-Plugins:INFO: dll-files: Found 1 file DLLs from numpy installation.
Nuitka-Postprocessing:INFO: Creating single file from dist folder, this may take a while.
Nuitka-Onefile:INFO: Running bootstrap binary compilation via Scons.
Nuitka-Scons:INFO: Onefile C compiler: cl (cl 14.1).
Nuitka-Scons:INFO: Onefile linking program with 1 files (no progress information available).
Nuitka-Scons:INFO: Compiled 1 C files using clcache with 0 cache hits and 1 cache misses.
Nuitka-Onefile:INFO: Using compression for onefile payload.
Nuitka-Onefile:INFO: Onefile payload compression ratio (25.24%) size 226839475 to 57256271.
Nuitka-Onefile:INFO: Keeping onefile build directory 'app.onefile-build'.
Nuitka:INFO: Keeping dist folder 'app.dist' for inspection, no need to use it.
Nuitka:INFO: Keeping build directory 'app.build'.
Nuitka:INFO: Successfully created 'app.exe'.

ワーニングはいくつか出たが、正しく動作する実行ファイルがビルドできた!
その後、PySide6のバージョン最新化、オプションなどを追加し、以下のコマンドで再ビルド

> nuitka --enable-plugin=pyside6 --standalone --onefile --follow-imports --nofollow-import-to=tkinter --disable-console app.py
Nuitka-Options:INFO: Used command line options: --enable-plugin=pyside6 --standalone --onefile --follow-imports --nofollow-import-to=tkinter --disable-console app.py
Nuitka:WARNING: The Python version '3.11' is only experimentally supported by Nuitka '1.5.8', but an upcoming release will change that. In the mean time use Python version '3.10' instead or newer Nuitka.
Nuitka-Options:INFO: Following all imports is the default for onefile mode and need not be specified.
Nuitka:INFO: Starting Python compilation with Nuitka '1.5.8' on Python '3.11' commercial grade 'not installed'.
Nuitka-Plugins:INFO: pyside6: Injecting pre-module load code for module 'PySide6':
Nuitka-Plugins:INFO: pyside6:     Adding binary folder to runtime 'PATH' environment variable for proper Qt loading.
Nuitka-Plugins:INFO: implicit-imports: Injecting post-module load code for module 'PySide6.QtCore':
Nuitka-Plugins:INFO: implicit-imports:     According to Yaml configuration.
Nuitka-Plugins:INFO: pyside6: Injecting post-module load code for module 'PySide6.QtCore':
Nuitka-Plugins:INFO: pyside6:     Setting Qt library path to distribution folder. We need to avoid loading target
Nuitka-Plugins:INFO: pyside6:     system Qt plugins, which may be from another Qt version.
Nuitka-Plugins:INFO: multiprocessing: Injecting pre-module load code for module 'multiprocessing':
Nuitka-Plugins:INFO: multiprocessing:     Monkey patching "multiprocessing" load environment.
Nuitka-Plugins:INFO: multiprocessing: Injecting post-module load code for module 'multiprocessing':
Nuitka-Plugins:INFO: multiprocessing:     Monkey patching "multiprocessing" for compiled methods.
Nuitka:INFO: Completed Python level compilation and optimization.                                  
Nuitka:INFO: Generating source code for C backend compiler.
Nuitka:INFO: Running data composer tool for optimal constant value handling.                                 
Nuitka:INFO: Running C compilation via Scons.
Nuitka-Scons:INFO: Backend C compiler: cl (cl 14.1).
Nuitka-Scons:INFO: Backend linking program with 406 files (no progress information available).
Nuitka-Scons:INFO: Compiled 406 C files using clcache with 405 cache hits and 1 cache misses.
Nuitka-Plugins:INFO: data-files: Included data file 'certifi\cacert.pem' due to package data for 'certifi'.
Nuitka-Plugins:INFO: pyside6: Including Qt plugins 'iconengines,imageformats,platforms,styles,tls' below 'PySide6\qt-plugins'.
Nuitka-Plugins:INFO: dll-files: Found 1 file DLLs from cv2 installation.
Nuitka-Plugins:INFO: dll-files: Found 1 file DLLs from numpy installation.
Nuitka-Postprocessing:INFO: Creating single file from dist folder, this may take a while.
Nuitka-Onefile:INFO: Running bootstrap binary compilation via Scons.
Nuitka-Scons:INFO: Onefile C compiler: cl (cl 14.1).
Nuitka-Scons:INFO: Onefile linking program with 1 files (no progress information available).
Nuitka-Scons:INFO: Compiled 1 C files using clcache with 0 cache hits and 1 cache misses.
Nuitka-Onefile:INFO: Using compression for onefile payload.
Nuitka-Onefile:INFO: Onefile payload compression ratio (25.05%) size 223104715 to 55882630.
Nuitka-Onefile:INFO: Keeping onefile build directory 'app.onefile-build'.
Nuitka:INFO: Keeping dist folder 'app.dist' for inspection, no need to use it.
Nuitka:INFO: Keeping build directory 'app.build'.
Nuitka:INFO: Successfully created 'app.exe'.

PySide6に関するワーニングが消えること、少しビルドサイズが小さくなることを確認、完

タイトルとURLをコピーしました