memorandum

pythonとか備忘録

PyInstallerでPythonスクリプトをexe化してみよう

Pythonが入ってないパソコンなどでPythonスクリプトを動かしたい

PyInstallerを使えばexe化することができます。PyInstallerという名前ですけど、インストーラというよりコンパイラに近いです。 www.pyinstaller.org

PyInstallerのインストール

pipでインストールできます。

$ pip install pyinstaller

オプション

オプションの簡単な説明

--windowed

コンソールを開かない

--onefile

関連するファイルをexeファイル1つにまとめたい

exe化してみる

オプションを付けてexe化してみましょう。 今回は以前作成したtest.pyを使用します。 下記の記事で作成したSQLiteを使ったスクリプトファイルです。 ten-sura.hatenablog.com

ライブラリ
(venv) E:\work\PycharmProjects\memorandum>pip freeze
altgraph==0.16.1
future==0.17.1
pefile==2019.4.18
PyInstaller==3.5
pywin32-ctypes==0.2.0
PyInstallerを実行
(venv) E:\work\PycharmProjects\memorandum>pyinstaller --windowed --onefile test.py
334 INFO: PyInstaller: 3.5
334 INFO: Python: 3.7.4
334 INFO: Platform: Windows-10-10.0.18362-SP0
334 INFO: wrote E:\work\PycharmProjects\memorandum\test.spec
334 INFO: UPX is not available.
339 INFO: Extending PYTHONPATH with paths
['E:\\work\\PycharmProjects\\memorandum',
 'E:\\work\\PycharmProjects\\memorandum']
339 INFO: checking Analysis
339 INFO: Building Analysis because Analysis-00.toc is non existent
339 INFO: Initializing module dependency graph...
344 INFO: Initializing module graph hooks...
349 INFO: Analyzing base_library.zip ...
4903 INFO: running Analysis Analysis-00.toc
4923 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executa
ble
  required by e:\work\pycharmprojects\memorandum\venv\scripts\python.exe
4943 INFO: Caching module hooks...
4948 INFO: Analyzing E:\work\PycharmProjects\memorandum\test.py
4988 INFO: Loading module hooks...
4988 INFO: Loading module hook "hook-encodings.py"...
5138 INFO: Loading module hook "hook-pydoc.py"...
5143 INFO: Loading module hook "hook-sqlite3.py"...
5334 INFO: Loading module hook "hook-xml.py"...
5629 INFO: Looking for ctypes DLLs
5629 INFO: Analyzing run-time hooks ...
5639 INFO: Looking for dynamic libraries
7293 INFO: Looking for eggs
7293 INFO: Using Python library C:\Users\********\AppData\Local\Programs\Python\Python37\pyt
hon37.dll
7293 INFO: Found binding redirects:
[]
7303 INFO: Warnings written to E:\work\PycharmProjects\memorandum\build\test\warn-test.txt
7348 INFO: Graph cross-reference written to E:\work\PycharmProjects\memorandum\build\test\xr
ef-test.html
7393 INFO: checking PYZ
7393 INFO: Building PYZ because PYZ-00.toc is non existent
7393 INFO: Building PYZ (ZlibArchive) E:\work\PycharmProjects\memorandum\build\test\PYZ-00.p
yz
7876 INFO: Building PYZ (ZlibArchive) E:\work\PycharmProjects\memorandum\build\test\PYZ-00.p
yz completed successfully.
7891 INFO: checking PKG
7891 INFO: Building PKG because PKG-00.toc is non existent
7891 INFO: Building PKG (CArchive) PKG-00.pkg
exeファイルはdistというフォルダに出力
(venv) E:\work\PycharmProjects\memorandum>dir dist
 ドライブ E のボリューム ラベルは ボリューム です
 ボリューム シリアル番号は ****-**** です

 E:\work\PycharmProjects\memorandum\dist のディレクトリ

2019/07/31  01:30    <DIR>          .
2019/07/31  01:30    <DIR>          ..
2019/07/31  01:30         6,296,710 test.exe
               1 個のファイル           6,296,710 バイト
               2 個のディレクトリ  ****  バイトの空き領域

Python本体と使用しているライブラリがexeファイルにまとめられるため、exファイルの容量が大きめとなります。

exeを実行してみる

生成したtest.exeを実行してみて、database.dbが出力されたら成功! DB Browser for SQLiteでデータベースの中身も確認しました。