A memo when the Python program [.py] is converted to an exe [.exe] to prevent it from being personalized. There are several ways to convert Python to an exe, but this time we will use PyInstaller, which can be easily done.
Reference: PyInstaller Quickstart
You can exe with pip and the following command without editing the source code. That's all, so it's very easy. If you just want to make it into an exe, this is fine. The latter half introduces how to write PyInstaller in Python code, so please refer to it if you are interested.
pip install pyinstaller
pyinstaller yourprogram.py
There are options that you can specify at run time. The frequently used options are summarized with reference to the following materials. Reference: [Using PyInstaller](https://pyinstaller.readthedocs.io/en/stable/usage.html https://gist.github.com/devlights/2ea169b60063d81f0526eceebeb7a8c6)
option | function |
---|---|
-n, --name | Specifying the name of the generated file(default is the script name) |
--specpath DIR | Specify generation folder(default is the current DIR) |
-D,--onedir | Output all in one directory |
-F,--onefile | Output in one file |
option | function |
---|---|
-w, --windowed, --noconsole | Hide the console |
-i, --icon | Program icon[.ico extension]To set |
option | function |
---|---|
--clean, --noconsole | Clear PyInstaller cache and delete temporary files before building |
-y, --noconfirm | Replace output directory without asking for confirmation |
-v, --version | Display the version information of the program and exit |
option | function |
---|---|
--exclude | Ignore the specified module or package. Can be used multiple times |
pyinstaller yourprogram.py --exclude numpy,-v, -i data.icon
Official document reference https://pyinstaller.readthedocs.io/en/stable/usage.html
sample.py
import PyInstaller.__main__
PyInstaller.__main__.run([
'--name=%s' % package_name,
'--onefile',
'--windowed',
'--add-binary=%s' % os.path.join('resource', 'path', '*.png'),
'--add-data=%s' % os.path.join('resource', 'path', '*.txt'),
'--icon=%s' % os.path.join('resource', 'path', 'icon.ico'),
os.path.join('my_package', '__main__.py'),
])
It was a memo of PyInstaller that can make Python exe. It's convenient to be able to do it in two lines. However, there is one concern. If you refer to the following, it seems that there is a problem that the exe file is large and it takes a long time to execute. If the target you want to exe does not consider the processing speed, please try it. [Sad news] PyInstaller will spit out a 300MB exe file
Thank you for reading. If you have any concerns, please make an edit request or comment. If you like, please also LGTM.
Recommended Posts