2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonスクリプトをexe化する方法|PyInstallerと--onedirで起動を高速化する手順

Posted at

Pythonスクリプトをexe化する方法と、起動を早くするためのポイント(PyInstaller編)

Pythonで作ったスクリプトを配布したい場合、**exeファイル化(実行ファイル化)**は非常に便利です。
この記事では、PyInstallerを使った exe の作成方法と、起動を早くするための --onedir オプションの活用方法をわかりやすく解説します。


✅ PyInstallerとは?

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

  • Python がインストールされていない PC でも動作可能
  • ライブラリや依存ファイルをまとめてパッケージ化
  • コマンド1つで exe を作成できる

✅ 1. PyInstaller のインストール

まずは PyInstaller をインストールします。

pip install pyinstaller

✅ 2. 基本的な exe の作成方法

最もシンプルなコマンドは以下です。

pyinstaller your_script.py

実行すると以下が生成されます:

  • dist/ … exe が入るフォルダ
  • build/ … ビルド時の一時ファイル
  • your_script.spec … ビルド設定ファイル

✅ 3. 起動を早くしたいなら「--onedir」を使う

PyInstaller には 「--onefile」 と 「--onedir」 の2種類のオプションがあります。

オプション 特徴
--onefile exe が1つにまとまるが、起動が遅い
--onedir 複数ファイルに分かれるが、起動が速い

🔍 なぜ --onedir の方が速いのか?
--onefile は起動時に 内部で zip を展開してから実行するため、どうしても起動が遅くなります。 一方 --onedir は展開済みの状態で配置されるため、即座に起動できます。

✅ 4. --onedir を使った exe 作成コマンド

起動を早くしたい場合は以下のコマンドを使います。

pyinstaller --onedir your_script.py

よく使うオプションを追加した例

pyinstaller --onedir --noconsole --icon=app.ico your_script.py
  • --noconsole … GUIアプリで黒いコンソールを出さない
  • --icon … アイコンを設定

✅ 5. フォルダ出力

dist配下にフォルダが生成されます。
このフォルダごと配布すれば、他のPCでも実行できます。

✅ 6. 起動速度の比較(体感)

方法 起動速度
--onefile 遅い(展開処理が入る)
--onedir 速い(即起動)

✅ 7. どちらを使うべき?

  • 起動速度を重視 → --onedir
  • 配布のしやすさを重視 → --onefile

✅まとめ

  • PyInstaller を使えば Python スクリプトを簡単に exe 化できる
  • 起動を早くしたいなら --onedir が最適
  • --onefile は便利だが起動が遅くなる
2
0
1

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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?