PythonのプログラムをEXE化する方法
2021/01/22PythonPythonの基本,pyinstaller
PythonのプログラムをWindows上で動作するexeファイルにするにはpyinstallerを利用するのが簡単です。
pyinstallerは他のサードパーティー制のモジュールと同様に「pip」コマンドでインストールして使います。
pyinstallerの使い方
Pythonのプログラムは通常「.py」という拡張子のついた手テキストファイルから実行されることが多いですが、Windows上で動作するexeファイルにして他のコンピュータで動作させることができます。
pyinstallerのインストール
pyinstallerは他のサードパーティー制のモジュールと同様に「pip」コマンドでインストールして使います。
pip install pyinstaller
上記のコマンドを実行すると
Collecting pyinstaller Downloading pyinstaller-4.2.tar.gz (3.6 MB) |████████████████████████████████| 3.6 MB 6.8 MB/s Installing build dependencies ... done Getting requirements to build wheel ... done Preparing wheel metadata ... done Requirement already satisfied: setuptools in c:\python39\lib\site-packages (from pyinstaller) (49.2.1) Collecting pefile>=2017.8.1 Downloading pefile-2019.4.18.tar.gz (62 kB) |████████████████████████████████| 62 kB ... Collecting pyinstaller-hooks-contrib>=2020.6 Downloading pyinstaller_hooks_contrib-2020.11-py2.py3-none-any.whl (172 kB) |████████████████████████████████| 172 kB ... Collecting pywin32-ctypes>=0.2.0 Downloading pywin32_ctypes-0.2.0-py2.py3-none-any.whl (28 kB) Collecting altgraph Downloading altgraph-0.17-py2.py3-none-any.whl (21 kB) Collecting future Downloading future-0.18.2.tar.gz (829 kB) |████████████████████████████████| 829 kB ... Using legacy 'setup.py install' for pefile, since package 'wheel' is not installed. Using legacy 'setup.py install' for future, since package 'wheel' is not installed. Building wheels for collected packages: pyinstaller Building wheel for pyinstaller (PEP 517) ... done Created wheel for pyinstaller: filename=pyinstaller-4.2-py3-none-any.whl size=2413076 sha256=xxxxxx Stored in directory: c:\users\xxx\appdata\local\pip\cache\wheels\b2\10\66\xxxxxxxxxx Successfully built pyinstaller Installing collected packages: future, pywin32-ctypes, pyinstaller-hooks-contrib, pefile, altgraph, pyinstaller Running setup.py install for future ... done Running setup.py install for pefile ... done Successfully installed altgraph-0.17 future-0.18.2 pefile-2019.4.18 pyinstaller-4.2 pyinstaller-hooks-contrib-2020.11 pywin32-ctypes-0.2.0
のようなメッセージが表示されます。
これでpyinstallerのインストールは完了です。
pyinstallerの使い方
pyinstallerはexe化したいスクリプトファイルのあるフォルダーで以下のコマンドを入力します。
pyinstaller pythonファイル名 [--onefile] [--noconsole]
上記コマンドではオプションとして、–onefileと—noconsoleを指定しています。
–onefileは生成されるものを1つのファイルにまとめ、–noconsoleはexeファイル実行時にコンソールが表示されないようにします。
作成したexeファイルはPythonがインストールされていないWindowsパソコンでも動作させることができます。
exeファイルの最適化する方法
pyinstallerで作成したexeが処理の割には大きすぎる場合があります。
そんな時は処理に不要なモジュールをpipコマンドで組み込んでないか確認してみてください。
不要なモジュールを組み込んでいるPythonの環境でpyinstallerを使ってexeファイルを作成するとすべてのモジュールも含めたexeファイルが作成されます。
結果的に大きなexeファイルになってしまう場合があります。
pyinstallerでexe化する前に不要なモジュールをアンインストールするか、venvなどで仮想環境を作成して必要なものだけをインストールした環境でexe化しましょう。
venvについては以下の記事を参考にしてください。
まとめ
PythonのプログラムをWindows上で動作するexeファイルにするにはpyinstallerを利用するのが簡単です。
ただしexeファイルが巨大化しないように動作に必要な最小限のモジュールだけを使用するような環境で作成することをおススメします。
Posted by システムトラスト技術部X
関連記事

Pythonの内包表記を使ってリスト(list)や辞書(dict)を初期化する方法
Pythonの内包表記はリスト(list)や辞書(dict)を初期化するのに複数 ...

Pythonのwhile文でループする方法
Pythonではwhile文を使って処理を繰り返し行うループをつくることができま ...

Pythonのfor文でループする方法
Pythonの繰り返し処理はfor文を使って行います。Pythonのfor文はr ...

Pythonの関数への引数にデフォルト値を設定すると引数が省略できる
Pythonの関数への引数にデフォルト値を設定しておくと、呼び出し側で引数を省略 ...

PythonをWindowsパソコンにインストールする方法
WindowsのパソコンにPythonの実行環境を作成する方法です。Python ...