【Python】スクリプトをexe化する方法


この記事はプロモーションを含みます。

Pyinstallerを使って、Pythonスクリプトをexe化する方法を解説します。

Pyinstallerをインストールする

ここでは、Pyinstallerをインストールする方法を説明します。

インストール

以下のコマンドを実行し、Pyinstallerをインストールします。

5/30時点での最新バージョンは「6.6.0」ですが、exe化する際のオプションで「--noconsole」を指定するとエラーが発生するので、ここでは、「6.0.0」をインストールします。
最新版をインストールする場合は「pip install pyinstaller」を実行します。

PS Z:\Workspace\Test> pip install pyinstaller==6.0
Collecting pyinstaller==6.0
  Downloading pyinstaller-6.0.0-py3-none-win_amd64.whl.metadata (8.2 kB)
Requirement already satisfied: setuptools>=42.0.0 in c:\users\user\appdata\local\programs\python\python312\lib\site-packages (from pyinstaller==6.0) (69.5.1)
Requirement already satisfied: altgraph in c:\users\user\appdata\local\programs\python\python312\lib\site-packages (from pyinstaller==6.0) (0.17.4)
Requirement already satisfied: pyinstaller-hooks-contrib>=2021.4 in c:\users\user\appdata\local\programs\python\python312\lib\site-packages (from pyinstaller==6.0) (2024.6)
Requirement already satisfied: packaging>=20.0 in c:\users\user\appdata\local\programs\python\python312\lib\site-packages (from pyinstaller==6.0) (24.0)
Requirement already satisfied: pefile>=2022.5.30 in c:\users\user\appdata\local\programs\python\python312\lib\site-packages (from pyinstaller==6.0) (2023.2.7)
Requirement already satisfied: pywin32-ctypes>=0.2.1 in c:\users\user\appdata\local\programs\python\python312\lib\site-packages (from pyinstaller==6.0) (0.2.2)
Downloading pyinstaller-6.0.0-py3-none-win_amd64.whl (1.3 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.3/1.3 MB 8.1 MB/s eta 0:00:00
Installing collected packages: pyinstaller
  Attempting uninstall: pyinstaller
    Found existing installation: pyinstaller 6.6.0
    Uninstalling pyinstaller-6.6.0:
      Successfully uninstalled pyinstaller-6.6.0
Successfully installed pyinstaller-6.0.0

「Successfully installed pyinstaller-6.0.0」と表示されれば、インストールは完了となります。

バージョンの確認

Pyinstallerがインストールされているかを確認するため、バージョンの確認を行います。

以下のコマンドを実行し、バージョンが表示されれば、正しくインストールされています。

PS Z:\Workspace\Test> pyinstaller --version
6.0.0

スクリプトをexe化する

ここでは、スクリプトをexe化する方法を説明します。

テスト用のアプリを作成する

exe化後の動作確認のため、メッセージボックスを表示するだけの簡単なアプリを作成します。
ソースコードは以下となります。
ファイル名は「main.py」とします。

import tkinter.messagebox as messagebox
messagebox.showinfo('メッセージボックス', '処理を実行しました。')

exeファイルに変換する

前項で作成したスクリプトをexeファイルに変換します。
変換するには以下のようなコマンドを実行します。

pyinstaller --clean --noconsole --onefile main.py

コマンドの実行後、様々な情報が出力されますが、下記のようなメッセージが出力され、distフォルダ配下に、main.exeが作成されていれば変換は完了となります。

Building EXE from EXE-00.toc completed successfully.

動作確認

作成したアプリの動作確認を行います。
アプリを実行し、以下のような画面が表示されれば正しく動作しています。

コメント

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