pythonのソースをコンパイルして実行exeファイルを生成する手順

Python

1) 次のコマンドでpyinstallerライブラリをインストールします。

C:\work> python -m pip install pyinstaller

2) 次のコマンドでpyinstallerライブラリ及び依存ライブラリを確認します。

C:\work> pip freeze
altgraph==0.17.3
pefile==2023.2.7
pyinstaller==5.13.0
pyinstaller-hooks-contrib==2023.6
pywin32-ctypes==0.2.2

3)test.pyファイル名で次のテストソースを格納します。

print("Hello World")
input("Press Enter to exit...")

4)エラーがないことを確認するには、python.exe(python)またはpy.exe(py)でtest.pyを実行してみます。

C:\work> py test.py
Hello World
Press Enter to exit...

5)次のように、 pyinstallerコマンドを使用してtest.pyをコンパイルしてtest.exe実行モジュールを生成します。

C:\work> pyinstaller --onefile test.py
・・・中間省略・・・
9078 INFO: Building EXE from EXE-00.toc completed successfully.

6)次のコマンドでdistディレクトリにtest.exe実行モジュールが生成されていることを確認します。

C:\work> tree . /F
フォルダー パスの一覧
ボリューム シリアル番号は 424B-8DE5 です
C:\WORK
│  test.py
│  test.spec
├─build
│  └─test
│      │  Analysis-00.toc
│      │  base_library.zip
│      │  EXE-00.toc
│      │  PKG-00.toc
│      │  PYZ-00.pyz
│      │  PYZ-00.toc
│      │  test.exe.manifest
│      │  test.pkg
│      │  warn-test.txt
│      │  xref-test.html
│      │
│      └─localpycs
│              pyimod01_archive.pyc
│              pyimod02_importers.pyc
│              pyimod03_ctypes.pyc
│              pyimod04_pywin32.pyc
│              struct.pyc
└─dist
        test.exe

7)distディレクトリに生成されたtest.exeを次のように実行し、エラーなく結果が表示されたらpythonソースのコンパイルを成功です。

C:\work> .\dist\test.exe
Hello World
Press Enter to exit...

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