PythonでWindows上で動くGUIアプリを作ったのですが、Python環境がないパソコンでも使ってもらおうと、exeファイルの生成方法を調べました。
PyInstallerでexeファイルを生成できます。
- 公式:Using PyInstaller
Using PyInstaller — PyInstaller 6.11.1 documentation
環境
- Windows 10 64bit
- PyCharm 2019.3 (Community Edition)
- Python 3.7.3
- PyInstaller 3.5
作業は、すべてPyCharmのTerminal上で実施
手順
- pipでPyInstallerをインストール
> pip install pyinstaller
- PyInstallerでexeファイル生成
生成元のPythonファイルの場所で作業することを想定しています。> pyinstaller test.py --onefile
- オプションについて
たくさんありますが、すぐに使いそうなオプションだけ載せておきます。
詳細は、公式リファレンスを参照ください。
--onefile
:関連するファイルを1つにまとめてexeファイルを生成
--noconsole
:コンソール(コマンドプロンプト)が表示されないexeファイルを生成
- オプションについて
以下の2行が出ていれば、成功です。
319412 INFO: Appending archive to EXE [hogehoge]\dist\test.exe
319508 INFO: Building EXE from EXE-00.toc completed successfully.
作業ディレクトリのdist配下にexeファイルが生成されています。
実行して、動くことも確認しました。
やった。
Matplotlibを使用しているケースで、苦労している話もみていたので心配していましたが、ひとまず安心です。
PythonプログラムをExeファイル化出来なくて1日費やしたときには
https://qiita.com/mamon/items/01e5dc5c24cf9e81b689
リリースまでの残課題
- Version設定
Using PyInstaller — PyInstaller 6.11.1 documentation - 最適化フラグ(-O or -OO)の設定
Using PyInstaller — PyInstaller 6.11.1 documentation
あと、公式リファレンスをざっと読んだ感じ、Mac OS X向けにもビルドできるっぽい
つづき
実はビルド時、エラーがでています。。。
動いていいの?確認せねば。。。
ビルドログ
> pyinstaller test.py --onefile
130 INFO: PyInstaller: 3.5
131 INFO: Python: 3.7.3
131 INFO: Platform: Windows-10-10.0.17763-SP0
137 INFO: wrote C:\Users\[USER]\PycharmProjects\pdf10\test.spec
141 INFO: UPX is not available.
144 INFO: Extending PYTHONPATH with paths
['C:\\Users\\[USER]\\PycharmProjects\\pdf10',
'C:\\Users\\[USER]\\PycharmProjects\\pdf10']
144 INFO: checking Analysis
146 INFO: Building Analysis because Analysis-00.toc is non existent
146 INFO: Initializing module dependency graph...
164 INFO: Initializing module graph hooks...
167 INFO: Analyzing base_library.zip ...
5896 INFO: running Analysis Analysis-00.toc
5949 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by c:\users\[USER]\appdata\local\programs\python\python37\python.exe
9762 INFO: Caching module hooks...
9775 INFO: Analyzing C:\Users\[USER]\PycharmProjects\pdf10\test.py
11104 INFO: Processing pre-find module path hook distutils
13456 INFO: Processing pre-find module path hook site
13458 INFO: site: retargeting to fake-dir 'c:\\users\\[USER]\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\PyInstaller\\fake-modules'
17775 INFO: Processing pre-safe import module hook setuptools.extern.six.moves
40934 INFO: Processing pre-safe import module hook six.moves
60052 INFO: Loading module hooks...
60053 INFO: Loading module hook "hook-Crypto.py"...
60086 INFO: Loading module hook "hook-distutils.py"...
60092 INFO: Loading module hook "hook-django.core.cache.py"...
60391 INFO: Loading module hook "hook-django.core.mail.py"...
60483 INFO: Loading module hook "hook-django.core.management.py"...
60578 INFO: Excluding import 'IPython'
60581 INFO: Excluding import 'matplotlib'
60585 INFO: Excluding import 'tkinter'
60588 INFO: Loading module hook "hook-django.db.backends.py"...
62218 WARNING: Hidden import "django.db.backends.__pycache__.base" not found!
62219 INFO: Loading module hook "hook-django.py"...
Traceback (most recent call last):
File "<string>", line 41, in <module>
File "<string>", line 36, in walk_packages
File "<string>", line 36, in walk_packages
File "<string>", line 20, in walk_packages
File "c:\users\[USER]\appdata\local\programs\python\python37\lib\site-packages\django\contrib\gis\admin\__init__.py", line 5, in <module>
from django.contrib.gis.admin.options import GeoModelAdmin, OSMGeoAdmin
File "c:\users\[USER]\appdata\local\programs\python\python37\lib\site-packages\django\contrib\gis\admin\options.py", line 2, in <module>
from django.contrib.gis.admin.widgets import OpenLayersWidget
File "c:\users\[USER]\appdata\local\programs\python\python37\lib\site-packages\django\contrib\gis\admin\widgets.py", line 3, in <module>
from django.contrib.gis.gdal import GDALException
File "c:\users\[USER]\appdata\local\programs\python\python37\lib\site-packages\django\contrib\gis\gdal\__init__.py", line 28, in <module>
from django.contrib.gis.gdal.datasource import DataSource
File "c:\users\[USER]\appdata\local\programs\python\python37\lib\site-packages\django\contrib\gis\gdal\datasource.py", line 39, in <module>
from django.contrib.gis.gdal.driver import Driver
File "c:\users\[USER]\appdata\local\programs\python\python37\lib\site-packages\django\contrib\gis\gdal\driver.py", line 5, in <module>
from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi
File "c:\users\[USER]\appdata\local\programs\python\python37\lib\site-packages\django\contrib\gis\gdal\prototypes\ds.py", line 9, in <module>
from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal
File "c:\users\[USER]\appdata\local\programs\python\python37\lib\site-packages\django\contrib\gis\gdal\libgdal.py", line 43, in <module>
% '", "'.join(lib_names)
django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal203", "gdal202", "gdal201", "gdal20", "gdal111"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.
67919 INFO: Determining a mapping of distributions to packages...
101696 INFO: Packages required by django:
['pytz', 'sqlparse']
102086 WARNING: No django root directory could be found!
117019 INFO: Processing pre-safe import module hook urllib3.packages.six.moves
119769 INFO: Loading module hook "hook-docutils.py"...
123583 INFO: Loading module hook "hook-encodings.py"...
123763 INFO: Loading module hook "hook-jinja2.py"...
123786 INFO: Loading module hook "hook-lib2to3.py"...
123799 INFO: Loading module hook "hook-matplotlib.backends.py"...
125151 INFO: Matplotlib backend "GTK3Agg": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
125775 INFO: Matplotlib backend "GTK3Cairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
126717 INFO: Matplotlib backend "MacOSX": ignored
cannot import name '_macosx' from 'matplotlib.backends' (C:\Users\[USER]\AppData\Roaming\Python\Python37\site-packages\matplotlib\backends\__init__.py)
127375 INFO: Matplotlib backend "nbAgg": ignored
No module named 'IPython'
128071 INFO: Matplotlib backend "Qt4Agg": ignored
Failed to import any qt binding
128746 INFO: Matplotlib backend "Qt4Cairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
129462 INFO: Matplotlib backend "Qt5Agg": ignored
Failed to import any qt binding
130152 INFO: Matplotlib backend "Qt5Cairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
131127 INFO: Matplotlib backend "TkAgg": added
131983 INFO: Matplotlib backend "TkCairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
132632 INFO: Matplotlib backend "WebAgg": ignored
Traceback (most recent call last):
File "C:\Users\[USER]\AppData\Roaming\Python\Python37\site-packages\matplotlib\backends\backend_webagg.py", line 26, in <module>
import tornado
ModuleNotFoundError: No module named 'tornado'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 12, in <module>
File "C:\Users\[USER]\AppData\Roaming\Python\Python37\site-packages\matplotlib\backends\backend_webagg.py", line 28, in <module>
raise RuntimeError("The WebAgg backend requires Tornado.")
RuntimeError: The WebAgg backend requires Tornado.
133465 INFO: Matplotlib backend "WX": ignored
No module named 'wx'
134103 INFO: Matplotlib backend "WXAgg": ignored
No module named 'wx'
134770 INFO: Matplotlib backend "WXCairo": ignored
No module named 'wx'
135437 INFO: Matplotlib backend "agg": added
136088 INFO: Matplotlib backend "cairo": ignored
cairo backend requires that pycairo>=1.11.0 or cairocffiis installed
136971 INFO: Matplotlib backend "pdf": added
137827 INFO: Matplotlib backend "pgf": added
138531 INFO: Matplotlib backend "ps": added
139207 INFO: Matplotlib backend "svg": added
140026 INFO: Matplotlib backend "template": added
140319 INFO: Loading module hook "hook-matplotlib.py"...
140971 INFO: Loading module hook "hook-numpy.core.py"...
141168 INFO: Loading module hook "hook-numpy.py"...
141172 INFO: Loading module hook "hook-openpyxl.py"...
141198 INFO: Loading module hook "hook-pandas.py"...
144769 INFO: Loading module hook "hook-PIL.Image.py"...
145367 INFO: Loading module hook "hook-PIL.py"...
145373 INFO: Import to be excluded not found: 'FixTk'
145373 INFO: Excluding import 'PySide'
145378 INFO: Removing import of PySide from module PIL.ImageQt
145379 INFO: Excluding import 'PyQt4'
145384 INFO: Removing import of PyQt4 from module PIL.ImageQt
145386 INFO: Excluding import 'tkinter'
145391 INFO: Removing import of tkinter from module PIL.ImageTk
145391 INFO: Excluding import 'PyQt5'
145397 INFO: Removing import of PyQt5 from module PIL.ImageQt
145398 INFO: Loading module hook "hook-PIL.SpiderImagePlugin.py"...
145405 INFO: Excluding import 'tkinter'
145409 INFO: Import to be excluded not found: 'FixTk'
145409 INFO: Loading module hook "hook-pkg_resources.py"...
146448 INFO: Processing pre-safe import module hook win32com
Traceback (most recent call last):
File "<string>", line 2, in <module>
ModuleNotFoundError: No module named 'win32com'
146620 INFO: Processing pre-safe import module hook win32com
Traceback (most recent call last):
File "<string>", line 2, in <module>
ModuleNotFoundError: No module named 'win32com'
146789 INFO: Loading module hook "hook-pycparser.py"...
146792 INFO: Loading module hook "hook-pydoc.py"...
146794 INFO: Loading module hook "hook-pygments.py"...
149751 INFO: Loading module hook "hook-pytz.py"...
149860 INFO: Loading module hook "hook-scipy.linalg.py"...
149864 INFO: Loading module hook "hook-scipy.py"...
149874 INFO: Loading module hook "hook-scipy.sparse.csgraph.py"...
149882 INFO: Loading module hook "hook-scipy.special._ellip_harm_2.py"...
149885 INFO: Loading module hook "hook-scipy.special._ufuncs.py"...
149888 INFO: Loading module hook "hook-selenium.py"...
149900 INFO: Loading module hook "hook-setuptools.py"...
151090 INFO: Loading module hook "hook-sqlalchemy.py"...
151558 WARNING: Hidden import "pysqlite2" not found!
153019 INFO: Import to be excluded not found: 'sqlalchemy.testing'
153020 INFO: Loading module hook "hook-sqlite3.py"...
153245 INFO: Loading module hook "hook-sysconfig.py"...
153248 INFO: Loading module hook "hook-xml.dom.domreg.py"...
153251 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
153253 INFO: Loading module hook "hook-xml.py"...
153256 INFO: Loading module hook "hook-_tkinter.py"...
154319 INFO: checking Tree
154319 INFO: Building Tree because Tree-00.toc is non existent
154322 INFO: Building Tree Tree-00.toc
154532 INFO: checking Tree
154532 INFO: Building Tree because Tree-01.toc is non existent
154532 INFO: Building Tree Tree-01.toc
154558 INFO: Loading module hook "hook-django.db.backends.mysql.base.py"...
154566 INFO: Loading module hook "hook-django.db.backends.oracle.base.py"...
154800 INFO: Looking for ctypes DLLs
154922 INFO: Analyzing run-time hooks ...
154945 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
154954 INFO: Including run-time hook 'pyi_rth_pkgres.py'
154959 INFO: Including run-time hook 'pyi_rth_mplconfig.py'
154964 INFO: Including run-time hook 'pyi_rth_mpldata.py'
154969 INFO: Including run-time hook 'pyi_rth_django.py'
154975 INFO: Including run-time hook 'pyi_rth__tkinter.py'
155038 INFO: Looking for dynamic libraries
270255 INFO: Looking for eggs
270255 INFO: Using Python library c:\users\[USER]\appdata\local\programs\python\python37\python37.dll
270255 INFO: Found binding redirects:
[]
270286 INFO: Warnings written to C:\Users\[USER]\PycharmProjects\pdf10\build\test\warn-test.txt
270793 INFO: Graph cross-reference written to C:\Users\[USER]\PycharmProjects\pdf10\build\test\xref-test.html
271148 INFO: checking PYZ
271148 INFO: Building PYZ because PYZ-00.toc is non existent
271148 INFO: Building PYZ (ZlibArchive) C:\Users\[USER]\PycharmProjects\pdf10\build\test\PYZ-00.pyz
Syntax error in c:\users\[USER]\appdata\local\programs\python\python37\lib\site-packages\xlrd\__init__.py
('invalid syntax', ('c:\\users\\[USER]\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\xlrd\\__init__.py', 1187, 41, ' print "EXTERNSHEET(b7-):"\n'))
275417 INFO: Building PYZ (ZlibArchive) C:\Users\[USER]\PycharmProjects\pdf10\build\test\PYZ-00.pyz completed successfully.
275537 INFO: checking PKG
275537 INFO: Building PKG because PKG-00.toc is non existent
275537 INFO: Building PKG (CArchive) PKG-00.pkg
319158 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
319412 INFO: Bootloader c:\users\[USER]\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
319412 INFO: checking EXE
319412 INFO: Building EXE because EXE-00.toc is non existent
319412 INFO: Building EXE from EXE-00.toc
319412 INFO: Appending archive to EXE C:\Users\[USER]\PycharmProjects\pdf10\dist\test.exe
319508 INFO: Building EXE from EXE-00.toc completed successfully.
C:\Users\[USER]\PycharmProjects\pdf10>
C:\Users\[USER]\PycharmProjects\pdf10>
エラー内容と対応
エラーを抜粋すると4件かな。
1.
django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal203", "gdal202", "gdal201", "gdal20", "gdal111"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.
- GeoDjangoのインストールで対応??
https://homata.gitbook.io/geodjango/geodjango/install
未確認です。。。たぶん行けそうですが。。。
2.
ModuleNotFoundError: No module named 'tornado'
- tornado インストールで対応
> pip install tornado
3.
ModuleNotFoundError: No module named 'win32com'
- win32com インストールで対応
https://github.com/mhammond/pywin32/releases
PythonバージョンとOSにあったバージョンをダウンロードしてインストールする
4.
Syntax error in c:\users\[USER]\appdata\local\programs\python\python37\lib\site-packages\xlrd\__init__.py
('invalid syntax', ('c:\\users\\[USER]\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\xlrd\\__init__.py', 1187, 41, ' print "EXTERNSHEET(b7-):"\n'))
これから調べてみる。
コメント