![](http://python-guide.work/wp-content/themes/cocoon-master/images/b-woman.png)
Pythonプログラムをexe化できます!
![](http://python-guide.work/wp-content/themes/cocoon-master/images/man.png)
exe化することで
・Pythonの環境環境が無くても
・Pythonのプログラムを実行
できます!
![](http://python-guide.work/wp-content/themes/cocoon-master/images/b-woman.png)
この記事では
・外部パッケージ「pyinstaller」を使用してexe化する方法
を紹介します!
※外部パッケージ「nuitka」でもexe化できますが、今回は割愛します。
前提
外部パッケージ「pyinstaller」がインストールされていること。
pipコマンドでインストールする場合、以下でインストールできます。
pip install pyinstaller
exe化するPythonプログラム
ここでは例として
・メッセージ「Pythonプログラムを実行!」を表示
します。
import tkinter.messagebox as messagebox
def main_func():
messagebox.showinfo("メッセージ", "Pythonプログラムを実行!")
if __name__ == "__main__":
main_func()
Pythonプログラムをexe化
以下のコマンドを実行します。
●構文
pyinstaller exe化するPythonプログラムのファイル名 --onefile --noconsole
●実行例
pyinstaller sample.py --onefile --noconsole
実行結果
Pythonプログラムをexe化できました。
※「~ completed successfully」により、exe化が成功したことを確認できます。
![実行結果①](https://python-guide.work/wp-content/uploads/2024/02/py_exe_pyinstaller_001.png)
![実行結果②](https://python-guide.work/wp-content/uploads/2024/02/py_exe_pyinstaller_002.png)
以下の3つが作成されます。
・「dist」フォルダ
・「build」フォルダ
・「sample.spec」ファイル
![実行結果③](https://python-guide.work/wp-content/uploads/2024/02/py_exe_pyinstaller_003.png)
上記のうちの「dist」フォルダの中に、exeファイルが作成されています。
![実行結果④](https://python-guide.work/wp-content/uploads/2024/02/py_exe_pyinstaller_004.png)
作成されたexeファイルを実行すると、Pythonプログラムが実行されました。
![実行結果⑤](https://python-guide.work/wp-content/uploads/2024/02/py_exe_pyinstaller_005.png)
参考
外部パッケージ「pyinstaller」の詳細は、公式サイトをご確認ください