勉強しないとな~blog

ちゃんと勉強せねば…な電気設計エンジニアです。

OpenCVやってみる - 54. Nuitkaでexe化

今回は、前回に引き続きexe化をやります。 前はPyinstallerを使いましたが、今回はNuitkaを試してみます。

conda環境作成

前回用意したconda環境はかなり最小限のものでしたが、Pyinstallerがどうしても余分になってしまいます。
なので、環境を再作成します。

前と同じコマンドとなります。
実行結果は省略します。

conda create -n harupan_nuitka python=3.7
conda activate harupan_nuitka
pip install opencv-python Pillow

Nuitkaインストール

pipでインストールできます。

参考サイト
NuitkaでPythonプログラムを配布してみよう | インフォメーション・ディベロプメント

pip install nuitka

実行結果

(harupan_nuitka) C:\Users\a\work\OpenCV\harupan\harupan_data>pip install nuitka
Collecting nuitka
  Downloading Nuitka-1.5.6.tar.gz (4.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.2/4.2 MB 6.3 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Collecting zstandard>=0.15
  Downloading zstandard-0.20.0-cp37-cp37m-win_amd64.whl (644 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 644.5/644.5 kB 5.8 MB/s eta 0:00:00
Collecting ordered-set>=4.1.0
  Downloading ordered_set-4.1.0-py3-none-any.whl (7.6 kB)
Building wheels for collected packages: nuitka
  Building wheel for nuitka (pyproject.toml) ... done
  Created wheel for nuitka: filename=Nuitka-1.5.6-cp37-cp37m-win_amd64.whl size=2874116 sha256=ae2e48989ca850acd83b60a16242cd3fd7fb351a8f123dc3a6e7647a4128cef7
  Stored in directory: c:\users\a\appdata\local\pip\cache\wheels\1a\93\6b\a0109964271ae49d17bf0845da846148a392cf697099275638
Successfully built nuitka
Installing collected packages: zstandard, ordered-set, nuitka
Successfully installed nuitka-1.5.6 ordered-set-4.1.0 zstandard-0.20.0

(harupan_nuitka) 

Nuitka実行

Nuitkaの使い方は、下記サイトで特に詳しく説明されています。

Nuitkaを使ってスクリプトをバイナリ化してみよう - PythonOsaka

今回は、下記コマンドでexe生成しました。

nuitka harupan.py --standalone --enable-plugin=tk-inter --include-data-dir=data=data --output-dir=nuitka --disable-console
  • --standalone
    生成したexeを実行するPCにPython環境がなくても実行できるようになります。
  • --enable-plugin=tk-inter
    tkinterを使う場合は、このオプションが必要になります。
    numpyも--enable-pluginのオプションを付ける必要がある、と書かれているサイトもありましたが、今回はなしで大丈夫でした。
  • --include-data-dir=data=data
    今回は外部のデータファイルを含みますが、そのフォルダを指定します。
    フォルダ内の全ファイルが配布用フォルダにコピーされます。
    オプション内で=が2回現れていますが、1つ目の後がコピー元、2つ目の後がコピー先になります。
  • --output-dir
    ビルド生成物フォルダと、exeを含む配布用フォルダの生成先を指定します。
  • --disable-console
    exe実行時、コンソールが出現しないようになります。

なお、--onefileオプションを付けると、Pyinstallerのときと同様、1つのexeファイルに集約されるとのことですが、やってみたところ、exe生成に失敗してしまいました…
今回は諦めにします。

実行結果(これまた長いですが、今後の参考として、全部掲載しちゃいます。)

(harupan_nuitka) C:\Users\a\work\OpenCV\harupan\harupan_data>nuitka harupan.py --standalone --enable-plugin=tk-inter --include-data-dir=data=data --output-dir=nuitka --disable-console 
Nuitka-Options:INFO: Used command line options: harupan.py --standalone --enable-plugin=tk-inter --include-data-dir=data=data --output-dir=nuitka --disable-console
Nuitka:INFO: Starting Python compilation with Nuitka '1.5.6' on Python '3.7' commercial grade 'not installed'.
Nuitka-Plugins:INFO: tk-inter: Injecting pre-module load code for module 'tkinter':
Nuitka-Plugins:INFO: tk-inter:     Need to make sure we set environment variables for TCL.
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: Too old gcc 'C:\Program Files\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin\gcc.exe' ((8, 1, 0) < (11, 2)) ignored!
Nuitka will use gcc from MinGW64 of winlibs to compile on Windows.

Is it OK to download and put it in 'C:\Users\a\AppData\Local\Nuitka\Nuitka\Cache\downloads\gcc\x86_64\11.3.0-14.0.3-10.0.0-msvcrt-r3'.

Fully automatic, cached. Proceed and download? [Yes]/No :
Nuitka:INFO: Downloading 'https://github.com/brechtsanders/winlibs_mingw/releases/download/11.3.0-14.0.3-10.0.0-msvcrt-r3/winlibs-x86_64-posix-seh-gcc-11.3.0-llvm-14.0.3-mingw-w64msvcrt-10.0.0-r3.zip'.
Nuitka:INFO: Extracting to 'C:\Users\a\AppData\Local\Nuitka\Nuitka\Cache\downloads\gcc\x86_64\11.3.0-14.0.3-10.0.0-msvcrt-r3\mingw64\bin\gcc.exe'
Nuitka-Scons:INFO: Backend C compiler: gcc (gcc).
Nuitka will make use of ccache to speed up repeated compilation.

Is it OK to download and put it in 'C:\Users\a\AppData\Local\Nuitka\Nuitka\Cache\downloads\ccache\v4.6'.

Fully automatic, cached. Proceed and download? [Yes]/No :
Nuitka:INFO: Downloading 'https://github.com/ccache/ccache/releases/download/v4.6/ccache-4.6-windows-32.zip'.
Nuitka:INFO: Extracting to 'C:\Users\a\AppData\Local\Nuitka\Nuitka\Cache\downloads\ccache\v4.6\ccache.exe'
Nuitka-Scons:INFO: Slow C compilation detected, used 60s so far, scalability problem.
Nuitka-Scons:INFO: Running '"C:\\Users\\a\\AppData\\Local\\Nuitka\\Nuitka\\Cache\\downloads\\ccache\\v4.6\\ccache.exe" "C:\\Users\\a\\AppData\\Local\\Nuitka\\Nuitka\\Cache\\downloads\\gcc\\x86_64\\11.3.0-14.0.3-10.0.0-msvcrt-r3\\mingw64\\bin\\gcc.exe" -o "module.PIL.Image.o" -c -std=c11 -fvisibility=hidden -fwrapv -pipe -fpartial-inlining -ftrack-macro-expansion=0 -Wno-deprecated-declarations -fno-var-tracking -Wno-misleading-indentation -fcompare-debug-second -O3 -D_WIN32_WINNT=0x0501 -D__NUITKA_NO_ASSERT__ -D_NUITKA_WINMAIN_ENTRY_POINT -D_NUITKA_STANDALONE -DMS_WIN64 -D_NUITKA_CONSTANTS_FROM_RESOURCE -D_NUITKA_FROZEN=155 -D_NUITKA_EXE -IC:\\Users\\a\\anaconda3\\envs\\harupan_nuitka\\include -I. -IC:\\Users\\a\\ANACON~1\\envs\\HARUPA~1\\lib\\SITE-P~1\\nuitka\\build\\include -IC:\\Users\\a\\ANACON~1\\envs\\HARUPA~1\\lib\\SITE-P~1\\nuitka\\build\\static_src "module.PIL.Image.c"' took 78.11 seconds
Nuitka-Scons:INFO: Slow C compilation detected, used 60s so far, scalability problem.
Nuitka-Scons:INFO: Running '"C:\\Users\\a\\AppData\\Local\\Nuitka\\Nuitka\\Cache\\downloads\\ccache\\v4.6\\ccache.exe" "C:\\Users\\a\\AppData\\Local\\Nuitka\\Nuitka\\Cache\\downloads\\gcc\\x86_64\\11.3.0-14.0.3-10.0.0-msvcrt-r3\\mingw64\\bin\\gcc.exe" -o "module.PIL.TiffImagePlugin.o" -c -std=c11 -fvisibility=hidden -fwrapv -pipe -fpartial-inlining -ftrack-macro-expansion=0 -Wno-deprecated-declarations -fno-var-tracking -Wno-misleading-indentation -fcompare-debug-second -O3 -D_WIN32_WINNT=0x0501 -D__NUITKA_NO_ASSERT__ -D_NUITKA_WINMAIN_ENTRY_POINT -D_NUITKA_STANDALONE -DMS_WIN64 -D_NUITKA_CONSTANTS_FROM_RESOURCE -D_NUITKA_FROZEN=155 -D_NUITKA_EXE -IC:\\Users\\a\\anaconda3\\envs\\harupan_nuitka\\include -I. -IC:\\Users\\a\\ANACON~1\\envs\\HARUPA~1\\lib\\SITE-P~1\\nuitka\\build\\include -IC:\\Users\\a\\ANACON~1\\envs\\HARUPA~1\\lib\\SITE-P~1\\nuitka\\build\\static_src "module.PIL.TiffImagePlugin.c"' took 81.07 seconds
Nuitka-Scons:INFO: Slow C compilation detected, used 60s so far, scalability problem.
Nuitka-Scons:INFO: Running '"C:\\Users\\a\\AppData\\Local\\Nuitka\\Nuitka\\Cache\\downloads\\ccache\\v4.6\\ccache.exe" "C:\\Users\\a\\AppData\\Local\\Nuitka\\Nuitka\\Cache\\downloads\\gcc\\x86_64\\11.3.0-14.0.3-10.0.0-msvcrt-r3\\mingw64\\bin\\gcc.exe" -o "module.numpy.lib.npyio.o" -c -std=c11 -fvisibility=hidden -fwrapv -pipe -fpartial-inlining -ftrack-macro-expansion=0 -Wno-deprecated-declarations -fno-var-tracking -Wno-misleading-indentation -fcompare-debug-second -O3 -D_WIN32_WINNT=0x0501 -D__NUITKA_NO_ASSERT__ -D_NUITKA_WINMAIN_ENTRY_POINT -D_NUITKA_STANDALONE -DMS_WIN64 -D_NUITKA_CONSTANTS_FROM_RESOURCE -D_NUITKA_FROZEN=155 -D_NUITKA_EXE -IC:\\Users\\a\\anaconda3\\envs\\harupan_nuitka\\include -I. -IC:\\Users\\a\\ANACON~1\\envs\\HARUPA~1\\lib\\SITE-P~1\\nuitka\\build\\include -IC:\\Users\\a\\ANACON~1\\envs\\HARUPA~1\\lib\\SITE-P~1\\nuitka\\build\\static_src "module.numpy.lib.npyio.c"' took 81.81 seconds
Nuitka-Scons:INFO: Slow C compilation detected, used 60s so far, scalability problem.
Nuitka-Scons:INFO: Running '"C:\\Users\\a\\AppData\\Local\\Nuitka\\Nuitka\\Cache\\downloads\\ccache\\v4.6\\ccache.exe" "C:\\Users\\a\\AppData\\Local\\Nuitka\\Nuitka\\Cache\\downloads\\gcc\\x86_64\\11.3.0-14.0.3-10.0.0-msvcrt-r3\\mingw64\\bin\\gcc.exe" -o "module.numpy.ma.core.o" -c -std=c11 -fvisibility=hidden -fwrapv -pipe -fpartial-inlining -ftrack-macro-expansion=0 -Wno-deprecated-declarations -fno-var-tracking -Wno-misleading-indentation -fcompare-debug-second -O3 -D_WIN32_WINNT=0x0501 -D__NUITKA_NO_ASSERT__ -D_NUITKA_WINMAIN_ENTRY_POINT -D_NUITKA_STANDALONE -DMS_WIN64 -D_NUITKA_CONSTANTS_FROM_RESOURCE -D_NUITKA_FROZEN=155 -D_NUITKA_EXE -IC:\\Users\\a\\anaconda3\\envs\\harupan_nuitka\\include -I. -IC:\\Users\\a\\ANACON~1\\envs\\HARUPA~1\\lib\\SITE-P~1\\nuitka\\build\\include -IC:\\Users\\a\\ANACON~1\\envs\\HARUPA~1\\lib\\SITE-P~1\\nuitka\\build\\static_src "module.numpy.ma.core.c"' took 96.54 seconds
Nuitka-Scons:INFO: Backend linking program with 176 files (no progress information available).
Nuitka-Scons:INFO: Compiled 704 C files using ccache.
Nuitka-Scons:INFO: Cached C files (using ccache) with result 'cache miss': 176
Nuitka-Options:INFO: Included data file 'data\harupan_svm.dat' due to specified data dir 'data' on command line.
Nuitka-Options:INFO: Included data file 'data\harupan_svm_220412.dat' due to specified data dir 'data' on command line.       
Nuitka-Options:INFO: Included data file 'data\templates2019.json' due to specified data dir 'data' on command line.
Nuitka-Options:INFO: Included data file 'data\templates2019_220412.json' due to specified data dir 'data' on command line.    
Nuitka-Options:INFO: Included data file 'data\templates2020.json' due to specified data dir 'data' on command line.
Nuitka-Options:INFO: Included data file 'data\templates2020_220412.json' due to specified data dir 'data' on command line.    
Nuitka-Options:INFO: Included data file 'data\templates2021.json' due to specified data dir 'data' on command line.
Nuitka-Options:INFO: Included data file 'data\templates2021_220412.json' due to specified data dir 'data' on command line.    
Nuitka-Plugins:INFO: tk-inter: Included 87 data files due to Tk needed for tkinter usage.
Nuitka-Plugins:INFO: tk-inter: Included 830 data files due to Tcl needed for tkinter usage.
Nuitka-Plugins:INFO: dll-files: Found 1 file DLLs from cv2 installation.
Nuitka-Plugins:WARNING: dll-files: DLL configuration by filename code for 'numpy' did not give a result. Either conditions are
Nuitka-Plugins:WARNING: missing, or this version of the module needs treatment added.
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\a\AppData\Local\Nuitka\Nuitka\Cache\downloads\depends\x86_64'.

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

(harupan_nuitka) C:\Users\a\work\OpenCV\harupan\harupan_data> 

生成フォルダ構成

(アプリルート)  
└─ nuitka
     ├─ harupan.build
     │   └─ ビルド生成物色々
     └─ harupan.dist    ... フォルダサイズ 209MB !
          ├─ harupan.exe
          ├─ data
          │   ├─ harupan_svm_220412.dat
          │   ├─ templates2021.json
          │   └─ ...
          └─ その他exe実行に必要なファイル、フォルダ
  • 途中、gcc、ccache、Dependency Walkerをダウンロードしていいかの確認が入りました。
    C:\Users\(ユーザ名)\AppData\Local\Nuitkaの下にダウンロードしたものが置かれていました。
    この後何度かexe生成をやったところでは、これらが再利用されているようでした。
  • 上記ダウンロードを含めて、10分ほどかかりました。
    再実行したときは、4分ほどの所要時間でした。少し待ち時間が出る感じです。
  • 生成されたフォルダサイズを見ると、Pyinstallerのときより大きくなっていました。
    • Pyinstaller: 157MB
    • Nuitka: 209MB

exe実行

harupan.distフォルダ内のharupan.exeをダブルクリックで実行。

動きました。

ただ、処理速度が速くなることを期待しましたが、特にそんなことはなく。

ここまで

今回はこんなところで。
Nuitkaを使ってみましたが、期待したような処理速度向上はなく、サイズも大きくなり、単一exe生成も失敗してしまったので、Pyinstallerで生成したexeを使うことにします。

このexeをGitHubで公開して一区切りかな。