0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

🚀【初心者でも5分でできる】PythonスクリプトをPyInstallerで実行ファイル(.exe)に変換してみよう!

Posted at

✅ PyInstallerってなに?

PyInstaller は、Pythonスクリプトを Windowsの実行ファイル(.exe)などに変換してくれるツール です。

特徴

  • PythonがインストールされていないPCでも動かせる!
  • .exeファイルを1つにまとめて配布できる!
  • GUIアプリでもOK(Tkinter, PyQtなど対応)

🧱 事前準備:インストールしよう

まずはPyInstallerをインストールします。コマンドプロンプトまたはターミナルで以下を実行👇

pip install pyinstaller

🐍 スクリプトを.exeに変換する手順

例として、以下のようなPythonスクリプト(hello.py)を用意します:

# hello.py
print("Hello, PyInstaller!")
input("Enterキーで終了します")

🔁 ① 基本コマンドで変換

pyinstaller hello.py

実行後、次のようなフォルダ構成になります:

dist/
 └─ hello/
      └─ hello.exe ← 実行ファイル!
build/
hello.spec

📦 ② .exe を1ファイルにまとめたい場合

pyinstaller --onefile hello.py

これで dist/hello.exe に1つのファイルだけが出力されます。


🖼️ GUIアプリでコンソールを表示させたくないとき

GUIアプリ(Tkinterなど)で黒いコンソール画面を出さずに実行したい場合は:

pyinstaller --onefile --noconsole hello_gui.py

📂 出力ファイルの確認

フォルダ/ファイル 説明
dist/ .exe ファイルが入っているフォルダ
build/ 一時ファイル(削除してOK)
.spec ビルド設定ファイル(高度なカスタム用)

🧹 後片付け(不要なファイルを削除)

次のコマンドでスッキリさせましょう:

rm -r build hello.spec __pycache__

Windowsなら手動削除でもOKです。


🧪 実行ファイルを試してみよう

コマンドラインで実行:

dist\hello.exe

または、エクスプローラーで hello.exe をダブルクリック!


🧠 よくあるエラーと対処法

エラー内容 原因 解決策
.exeが開かない 依存モジュールが不足 すべての pip install を済ませてから変換
セキュリティ警告が出る ウイルス誤検出 zipで配布 or セキュリティ設定で許可
日本語が文字化けする エンコーディング問題 ファイル先頭に # -*- coding: utf-8 -*- を追加

📌 実際に使ってみて感じたポイント

  • --onefile オプションが超便利(配布に最適)
  • GUIアプリには --noconsole を付けるのがマナー
  • .spec を使うとアイコンやリソースのカスタマイズも可能(※上級者向け)

📚 参考リンク


✍️ まとめ

  • PyInstallerを使えば、Pythonスクリプトを簡単に実行ファイル化できる
  • .exeにすれば、Pythonが入っていないPCでも配布&実行OK
  • GUI/CLI問わず対応。トラブル対策も押さえておこう!

🙋‍♀️ こんな人におすすめ

  • Pythonで作ったツールを知人に配布したい
  • CLIツールを会社のPCで使いたい
  • TkinterやPyQtなどでGUIアプリを作ったけど、配布方法に悩んでいる
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?