亀の甲羅2

今日もまた朝とく起きて励まなん窓に明るきありあけの月

Python:pythonのexe化

pythonでもコンパイルして(?)exeロードモジュールを作成できることを知った。exe化できれば、作成したpythonコードをpythonが無くても実行できることになり、実行環境の幅が広がる。今まで何らかの外部モジュールをインストールしなければ実行できないものと思っていたが、exeできるならより汎用的に使用できることになることになり、pythonにより作成されたプログラムを配布しやすくなる。

目次

本文

1. コンパイラ?インストール(pyinstaller)

PS C:\Users\xxxxx\Desktop> pip install pyinstaller
Collecting pyinstaller
  Downloading pyinstaller-4.7-py3-none-win_amd64.whl (2.0 MB)
     |████████████████████████████████| 2.0 MB 56 kB/s
Collecting pywin32-ctypes>=0.2.0
  Downloading pywin32_ctypes-0.2.0-py2.py3-none-any.whl (28 kB)
Collecting pyinstaller-hooks-contrib>=2020.6
  Downloading pyinstaller_hooks_contrib-2021.3-py2.py3-none-any.whl (200 kB)
     |████████████████████████████████| 200 kB 52 kB/s
Requirement already satisfied: setuptools in c:\python\lib\site-packages (from pyinstaller) (57.4.0)
Collecting altgraph
  Downloading altgraph-0.17.2-py2.py3-none-any.whl (21 kB)
Collecting pefile>=2017.8.1
  Downloading pefile-2021.9.3.tar.gz (72 kB)
     |████████████████████████████████| 72 kB 87 kB/s
  Preparing metadata (setup.py) ... done
Collecting future
  Downloading future-0.18.2.tar.gz (829 kB)
     |████████████████████████████████| 829 kB 142 kB/s
  Preparing metadata (setup.py) ... done
Using legacy 'setup.py install' for pefile, since package 'wheel' is not installed.
Using legacy 'setup.py install' for future, since package 'wheel' is not installed.
Installing collected packages: future, pywin32-ctypes, pyinstaller-hooks-contrib, pefile, altgraph, pyinstaller
    Running setup.py install for future ... done
    Running setup.py install for pefile ... done
Successfully installed altgraph-0.17.2 future-0.18.2 pefile-2021.9.3 pyinstaller-4.7 pyinstaller-hooks-contrib-2021.3 py

2. サンプルコードの準備

test.py

なにか適当なコードでOK!

3. exe化 (ロードモジュールの作成)

PS C:\Users\xxxxx\Desktop> pyinstaller test.py --onefile
359 INFO: PyInstaller: 4.7
359 INFO: Python: 3.9.7
458 INFO: Platform: Windows-10-10.0.19041-SP0
467 INFO: wrote C:\Users\xxxxx\Desktop\test.spec
477 INFO: UPX is not available.
487 INFO: Extending PYTHONPATH with paths
['C:\\Users\\xxxxx\\Desktop']
1341 INFO: checking Analysis
1342 INFO: Building Analysis because Analysis-00.toc is non existent
1342 INFO: Initializing module dependency graph...
1355 INFO: Caching module graph hooks...
1415 INFO: Analyzing base_library.zip ...
5955 INFO: Processing pre-find module path hook distutils from 'C:\\Python\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
5958 INFO: distutils: retargeting to non-venv dir 'C:\\Python\\lib'
11074 INFO: Caching module dependency graph...
11945 INFO: running Analysis Analysis-00.toc
11964 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Python\python.exe

<中略>

22648 INFO: Copying 0 resources to EXE
22648 INFO: Emedding manifest in EXE
22653 INFO: Updating manifest in C:\Users\xxxxx\Desktop\dist\test.exe
22656 INFO: Updating resource type 24 name 1 language 0
22709 INFO: Appending PKG archive to EXE
26014 INFO: Building EXE from EXE-00.toc completed successfully.

4. 出力ファイル

dist配下にexeができる。サイズが大きいので注意。

ファイル/フォルダ名                              バイト数   更新日付 更新時刻
───────────────────────────────────────
Root                                            < Folder >
├ build                                        < Folder >
│ └ test                                      < Folder >
│    ├ Analysis-00.toc                            24,783 2021/11/24 11:12:58
│    ├ base_library.zip                          782,710 2021/11/24 11:12:53
│    ├ EXE-00.toc                                  9,648 2021/11/24 11:13:08
│    ├ PKG-00.toc                                  8,131 2021/11/24 11:13:04
│    ├ PYZ-00.pyz                              1,749,443 2021/11/24 11:12:59
│    ├ PYZ-00.toc                                 17,052 2021/11/24 11:12:59
│    ├ test.exe.manifest                           1,492 2021/11/24 11:12:59
│    ├ test.pkg                                7,355,858 2021/11/24 11:13:04
│    ├ Tree-00.toc                                82,570 2021/11/24 11:12:57
│    ├ Tree-01.toc                                 6,997 2021/11/24 11:12:57
│    ├ Tree-02.toc                                   638 2021/11/24 11:12:58
│    ├ warn-test.txt                               3,957 2021/11/24 11:12:58
│    └ xref-test.html                            404,874 2021/11/24 11:12:58
└ dist                                         < Folder >
   └ test.exe                                   7,668,690 2021/11/24 11:13:08

   


参考

https://techacademy.jp/magazine/18963 https://www.python.ambitious-engineer.com/archives/3306 https://work-life-enj.com/use-pyinstaller/ https://qiita.com/konitech913/items/6259f13e057bc25ebc23