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入門
Python(パイソン)は、動的な型付けを行うインタープリター型のオブジェクト指 ...

プロキシー環境でPythonのpyinstallerがインストールできない時の対応
プロキシー環境でPythonのpyinstallerがインストールできない場合は ...

Pythonでのクラスの作り方
PythonはJavaやC++などとオブジェクト指向型言語と同様にClass(ク ...

Pythonの辞書(dict)に要素を追加・更新する方法
Pythonの辞書(dict)に、要素を追加・更新する方法は「辞書にキーを指定し ...

Pythonでクラスを多重継承する方法
Pythonではクラスの多重継承がサポートされており、サブクラス(子クラス)の宣 ...