LoginSignup
168
193

More than 5 years have passed since last update.

PyInstallerでexeファイル化

Last updated at Posted at 2019-03-13

業務で使用するPythonスクリプトを作っても、誰もがpythonがインストールされているとは限りません。
そんなときは、PyInstallerでWindows用のexeファイル(実行ファイル)に変換して配布するのがよいと思われます。

環境

  • OS : Windows 10 64bit
  • Python 3.6.0 (Anaconda3-4.3.1)
  • PyInstaller 3.4

PyInstallerのインストール

  1. コマンドプロンプトを起動
  2. pipでPyInstallerをインストール
$ pip install pyinstaller

exeファイルへの変換

ここでは、exe化したいpythonファイルをTest.py、作業フォルダをC:\PythonFilesとします。

  1. 作業フォルダに移動
  2. pyinstallerでexeファイルに変換
$ cd C:\PythonFiles
$ pyinstaller Test.py

結果

14719 INFO: Building COLLECT COLLECT-00.toc completed successfully.

実行後、completed successfullyと表示されたらexeファイル化完了です。
exeファイル化に成功すると、作業フォルダ内に下記のフォルダとファイルが生成されます。

  • pycache
  • build
  • dist
  • Test.spec

dist\Testフォルダ内にあるTest.exeファイルを実行するとスクリプトが走ります。
(配布するときはdistフォルダ毎配布します)

オプション

--onefile

onefileオプションを付けると、関連するファイルを1つにまとめてexeファイルを生成します。

$ pyinstaller Test.py --onefile

--noconsole

noconsoleオプションを付けると、コンソール(コマンドプロンプト)の黒い画面を表示しません。

$ pyinstaller Test.py --noconsole

その他備忘録

  • 1ファイル化したexeファイルは、1ファイル化していないexeよりも起動が遅いようです。
  • seleniumを含むpythonファイルをonefileオプションを付けて変換したところ、exeファイル化は成功しましたが、実行するとエラーになりました。seleniumを含むpythonファイルは1ファイル化できないようです。
  • noconsoleオプションを付けても、実行中にエラーが出るとコンソールが表示されます。
168
193
5

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
168
193