I posted it for the first time after recording it because I needed it for business and tried to find out and try it. I'm using py2exe and pyinstaller, and finally pyinstaller. All descriptions assume a virtual environment with pipenv. [^ 1] Please read as appropriate.
Please feel free to comment if there are any mistakes.
py2exe After a lot of research, it's lighter and faster than the pyinstaller! I tried to use it, but for some reason, if there are modules such as pandas and scipy, the dependent files are leaked and an import error occurs at runtime. (There was no problem with numpy. Also, if you check various things on Github etc., it seems that matplotlib and QtPy etc. can also be a problem.) I tried downgrading the library to match the release of the version of py2exe I was using (0.9.3.2), but it didn't improve. I once gave up on py2exe here.
I tried to execute it with a simple file, so I will explain only how to use it.
Download the .whl file from Github according to your environment and version. afterwards,
$ pipenv install py2exe-0.9.3.2-cp37-none-win_amd64.whl
#Under non-virtual environment
$ pip install py2exe-0.9.3.2-cp37-none-win_amd64.whl
Then you can install it.
(You can also install with pip install py2exe
, but it will be an older version.)
--Create setup.py in the same hierarchy as the file you want to exe (specify various options here. For details, please contact Google teacher).
setup.py
from distutils.core import setup
import setuptools
import py2exe
setup(
console = [{'script': 'xxx.py'}] #File to be exe
)
--exe conversion
$ pipenv run python setup.py py2exe
This will create hello.exe under the dist directory.
pyinstaller When using a library such as pandas or numpy, I avoided using it due to the double pain of being slower and larger than py2exe, but since py2exe was like the one described above, use this. To.
reference:
$ pipenv install pyinstaller
#Under non-virtual environment
$ pip install pyinstaller
--When converting files to exe without collecting them
$ pipenv run pyinstaller xxx.py
result
.
├─build
│ └─xxx
└─dist
└─xxx
├─xxx.exe #Generated executable file
├─Include
├─numpy
├─pandas
All less than xxx is required for execution.
--When converting files together into an exe
$ pipenv run pyinstaller xxx.py --onefile
result
.
├─build
│ └─xxx
└─dist
└─xxx.exe #Generated executable file
Refreshing.
Create a simple .py file and experiment. The environment is the same as above.
--Experimental pattern --'normal': exe without grouping files --'onefile': Files are combined into an exe
--Files used
hello.py
import sys
import numpy
import pandas
with open('test.txt', 'w') as f:
f.write(f'argv: {sys.argv}') #Check if the argument can be passed properly
--Execute
$ pipenv run pyinstaller hello.py # --onefile
$ dist/hello.exe hello world 1 2 3
--Output file
test.txt
argv: ['C:{PATH}\\hello.exe', 'hello', 'world', '1', '2', '3']
The way to pass arguments seems to be the same as executing .py.
--Result
File size(dist directory) | Execution time | |
---|---|---|
normal | 148MB | <1s |
onefile | 55MB | 10~20s |
There seems to be a trade-off between capacity and execution time. Even 55MB is big enough.
It may be used when incorporating a trained machine learning model into existing software written in a language other than python, but I am concerned about the size of the capacity and the dependency with other libraries not used this time. Where it becomes. If you feel like it, I will experiment a little more and add it. [^ 1]: Since there was a description that all the libraries in the environment are included in the exe at the time of exe conversion and the capacity becomes ridiculously large, use pipenv to install and execute the minimum library ing.
Recommended Posts