We will describe a series of methods to generate a GUI display executable file using Python. I used wxPython in the GUI library because it was easy to use as a license.
I am using Anaconda for the root environment. Since it is actually used in a virtual environment, I think that it is probably okay in other Conda-based environments.
Anaconda https://www.continuum.io/downloads
This time I want to run it in a test, so I will build a new virtual environment with the name wxenv without using the root environment. Run the following at the command prompt (Terminal on Mac) Note: As of May 2017, PyInstaller only supports Python 3.5.
command
conda create -n wxenv python=3.5.3
#When creating with a set of Anaconda
# conda create -n wxenv python=3.5.3 anaconda
#When checking the created environment
# conda info -e
# conda list -n wxenv
#When deleting the created virtual environment
# conda remove -n wxenv --all
Subsequent work is performed in the virtual environment.
command
# Windows
activate wxenv
#Deactivate
# deactivate
# Mac
source activate wxenv
#Deactivate
# source deactivate
WxPython
https://wxpython.org/
Basically, wxPython seems to be Python2 series, but I use wxPython that works with 3 series called Phoenix version. It is said that it supports PyPI from April 2017, and it seems that it can be installed by executing the following command.
command
pip install wxpython
Create a test executable file in a suitable location as shown below and save it.
main.py
#! env python
# -*- coding: utf-8 -*-
import wx
import sys,os
if __name__ == '__main__':
app = wx.App(False)
frame = wx.Frame(None, wx.ID_ANY, "Hello World")
frame.Show(True)
app.MainLoop()
PyInstaller
command
pip install pyinstaller
Windows For windows, also install the following
windows
pip install pypiwin32
First, output from the normal command prompt.
command
#Move to the save location of the test executable file generated earlier
cd (File storage location)
#Execute PyInstaller (1 directory, 1 file as an argument, an instruction to hide the console and erase the previous output result)
pyinstaller main.py -D -F -w --clean
When you run The generated exe file should have been generated in a folder called dist.
It is generated even if you run pyinstaller normally, but you may want to make fine adjustments at the time of generation. In that case, you can create a spec file, rewrite the file to a convenient setting, and then have pyinstaller read the setting and output it.
https://pythonhosted.org/PyInstaller/spec-files.html https://pythonhosted.org/PyInstaller/usage.html#options
command
#Move to the location of the test executable (unnecessary if already moved)
cd (File storage location)
#Generate spec file
#Target file name, spec file name to be generated, 1 directory, 1 file as an option, console hidden
pyi-makespec main.py -n="main" -D -F -w
Doing the above will generate a file called main.spec. In this file, there are each setting used by PyInstaller, which can be modified to specify an external file. If you cannot generate it well here, try deleting the part (-F) of one file. Also, some external libraries may need to be noted in the spec file. For details, see Official around Adding Binary Files.
command
#Specify a spec file and execute
pyinstaller main.spec
You should now have an exe file in a directory called dist.
When executing with PyInstaller, it is convenient to write the following contents in a Python file and make the directory containing the executable file the working directory.
main.py
os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))
For Mac, wxPython doesn't seem to work unless I use pythonw instead of python. http://www.sevencredit.com/2015/07/31/702/
Matplotlib
Even if Matplotlib is registered in PyInstaller, it often does not work with Qt links as it is. As a workaround, a different backend should work immediately after ʻimport matplotlib`.
import matplotlib
matplotlib.use('TkAgg')
#The following is omitted
https://github.com/pyinstaller/pyinstaller/issues/2857 http://chick.g.hatena.ne.jp/allegro/20091009/p3
WxFormBuilder
In my case, WxPython editing uses wxFormBuilder. https://qiita.com/mm_sys/items/716cb159ea8c9e634300
There was a way to operate lighter and faster than PyInstaller, so I created a separate article.
https://qiita.com/mm_sys/items/1fd3a50a930dac3db299
Recommended Posts