webmaster-kazu.com

テクノロジー・データ分析・実践ログ・その他

Pyinstallerを使用しPythonのスクリプトから実行可能ファイル(exeファイル)を作成する

カテゴリ: Python【初歩から現場実務的なもの】

Pyinstallerを使用しPythonのスクリプトから実行可能ファイル(exeファイル)を作成してみます。

なお、Pyinstallerモジュールは、Pythonの標準ライブラリではありませんので、事前にインストールする必要があります。

■Python

今回のPythonのバージョンは、「3.8.5」を使用しています。(Windows10)(pythonランチャーでの確認)

■Pythonのスクリプトを作成する

Pyinstallerを使用しPythonのスクリプトから実行可能ファイル(exeファイル)を作成する前に、まずはPythonのスクリプトを書いていきます。

■コード

import sys

import tkinter as tk

root = tk.Tk()

root.title(u"Sample Software")

root.geometry("400x300")

root.mainloop()

今回は400px * 300pxのウインドウでタイトルに「Sample Software」と表示される簡単なスクリプトを書きました。

■作成したスクリプトを保存する

今回のスクリプトを「software_sample.py」という名前で保存します。

今回ファイルを保存した場所は「C:\Users\user\test(フォルダパス)」です。この場所は、Pythonで実行可能ファイル(exeファイル)を作成する際に大切になりますので、覚えておきます。

■Pyinstallerを使用し実行可能ファイル(exeファイル)を作成する

スクリプトを保存した後は、Windowsのコマンドプロンプトを起動します。

C:\Users\user>cd test

起動後、スクリプトを保存した「C:\Users\user\test(フォルダパス)」に移動するため、cdコマンドを入力し、Enterキーを押します。

C:\Users\user\test>pyinstaller --onefile (作成したスクリプトの名前).py

Enterキーを押すと、スクリプトを保存した場所に移動しますので、その後に上記のコマンドを入力し、Enterキーを押します。

164 INFO: PyInstaller: 4.1

164 INFO: Python: 3.8.5

164 INFO: Platform: Windows-10-10.0.18362-SP0

166 INFO: wrote C:\Users\user\test\software_sample.spec

172 INFO: UPX is not available.

175 INFO: Extending PYTHONPATH with paths

['C:\\Users\\user\\test', 'C:\\Users\\user\\test']

241 INFO: checking Analysis

241 INFO: Building Analysis because Analysis-00.toc is non existent

241 INFO: Initializing module dependency graph...

250 INFO: Caching module graph hooks...

280 INFO: Analyzing base_library.zip ...

4081 INFO: Processing pre-find module path hook distutils from 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.

4083 INFO: distutils: retargeting to non-venv dir 'c:\\pg\\python38\\lib'

9129 INFO: Caching module dependency graph...

9297 INFO: running Analysis Analysis-00.toc

9322 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable

required by c:\pg\python38\python.exe

9783 INFO: Analyzing C:\Users\user\test\software_sample.py

9785 INFO: Processing module hooks...

9786 INFO: Loading module hook 'hook-difflib.py' from 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks'...

9789 INFO: Excluding import of doctest from module difflib

9790 INFO: Loading module hook 'hook-distutils.py' from 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks'...

9794 INFO: Loading module hook 'hook-distutils.util.py' from 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks'...

9796 INFO: Excluding import of lib2to3.refactor from module distutils.util

9797 INFO: Loading module hook 'hook-encodings.py' from 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks'...

9924 INFO: Loading module hook 'hook-heapq.py' from 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks'...

9927 INFO: Excluding import of doctest from module heapq

9927 INFO: Loading module hook 'hook-lib2to3.py' from 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks'...

10311 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks'...

10313 INFO: Excluding import of test.support from module multiprocessing.util

10314 INFO: Excluding import of test from module multiprocessing.util

10314 INFO: Loading module hook 'hook-pickle.py' from 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks'...

10317 INFO: Excluding import of argparse from module pickle

10317 INFO: Loading module hook 'hook-sysconfig.py' from 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks'...

10320 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks'...

10321 INFO: Loading module hook 'hook-xml.py' from 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks'...

10472 INFO: Loading module hook 'hook-_tkinter.py' from 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks'...

10729 INFO: checking Tree

10729 INFO: Building Tree because Tree-00.toc is non existent

10730 INFO: Building Tree Tree-00.toc

10869 INFO: checking Tree

10869 INFO: Building Tree because Tree-01.toc is non existent

10870 INFO: Building Tree Tree-01.toc

11015 INFO: checking Tree

11015 INFO: Building Tree because Tree-02.toc is non existent

11016 INFO: Building Tree Tree-02.toc

11039 INFO: Looking for ctypes DLLs

11080 INFO: Analyzing run-time hooks ...

11083 INFO: Including run-time hook 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'

11088 INFO: Including run-time hook 'c:\\pg\\python38\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth__tkinter.py'

11100 INFO: Looking for dynamic libraries

11433 INFO: Looking for eggs

11433 INFO: Using Python library c:\pg\python38\python38.dll

11434 INFO: Found binding redirects:

[]

11440 INFO: Warnings written to C:\Users\user\test\build\software_sample\warn-software_sample.txt

11490 INFO: Graph cross-reference written to C:\Users\user\test\build\software_sample\xref-software_sample.html

11523 INFO: checking PYZ

11524 INFO: Building PYZ because PYZ-00.toc is non existent

11525 INFO: Building PYZ (ZlibArchive) C:\Users\user\test\build\software_sample\PYZ-00.pyz

12281 INFO: Building PYZ (ZlibArchive) C:\Users\user\test\build\software_sample\PYZ-00.pyz completed successfully.

12302 INFO: checking PKG

12302 INFO: Building PKG because PKG-00.toc is non existent

12303 INFO: Building PKG (CArchive) PKG-00.pkg

15666 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.

15692 INFO: Bootloader c:\pg\python38\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe

15692 INFO: checking EXE

15693 INFO: Building EXE because EXE-00.toc is non existent

15693 INFO: Building EXE from EXE-00.toc

15695 INFO: Appending archive to EXE C:\Users\user\test\dist\software_sample.exe

15713 INFO: Building EXE from EXE-00.toc completed successfully.

Enterキーを押すと実行可能ファイル(exeファイル)の作成が開始され、上記のようなメッセージが表示されます。メッセージで「Building EXE from EXE-00.toc completed successfully.」といった表示がされれば、正常に実行可能ファイル(exeファイル)の作成が完了となります。

■実行可能ファイル(exeファイル)を実行する

作成が完了しましたので、検証のため、実行可能ファイル(exeファイル)を実行してみます。

まずはスクリプトを保存した場所を確認すると、「dist」というフォルダが作成されていることが確認できますので、このフォルダの中身を確認します。

フォルダの中身を確認すると、「software_sample.exe」という今回作成したスクリプトの名前と同じ実行可能ファイル(exeファイル)が確認できます。このファイルをダブルクリックで起動します。

起動すると、実行可能ファイル(exeファイル)が実行されます。