[PYTHON] How to turn a .py file into an .exe file

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.

environment

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.

Installation

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.)

Execution method

--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:

Installation

$ pipenv install pyinstaller

#Under non-virtual environment
$ pip install pyinstaller

Execution method

--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.

About the capacity and execution time of the .exe file generated by pyinstaller

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.

Summary

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

How to turn a .py file into an .exe file
How to convert Python to an exe file
How to paste a CSV file into an Excel file using Pandas
How to create a config file
[Python Kivy] How to create an exe file with pyinstaller
[Python] How to output a pandas table to an excel file
How to specify a .py file to load at startup in IPython 0.13
How to read a CSV file with Python 2/3
How to disguise a ZIP file as a PNG file
How to create a JSON file in Python
[Automation] Convert Python code into an exe file
How to read a file in a different directory
Backtrader How to import an indicator from another file
How to convert a mel spectrogram back to a wav file
How to deploy a Go application to an ECS instance
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
How to call a function
Upload a file to Dropbox
How to hack a terminal
How to put a hyperlink to "file: // hogehoge" with sphinx-> pdf
How to convert an array to a dictionary with Python [Application]
How to import a file anywhere you like in Python
How to generate a public key from an SSH private key
How to make a Python package (written for an intern)
How to check if a value exists in an enum
How to use a file other than .fabricrc as a configuration file
How to read an Excel file (.xlsx) with Pandas [Python]
How to create an ISO file (CD image) on Linux
How to connect the contents of a list into a string
How to make a Japanese-English translation
How to write a Python class
I tried to convert a Python file to EXE (Recursion error supported)
How to put a symbolic link
How to make a slack bot
How to create a Conda package
Write standard output to a file
How to make a command to read the configuration file with pyramid
How to create an email user
How to make a crawler --Advanced
How to make a recursive function
How to create a virtual bridge
How to create a heatmap with an arbitrary domain in Python
How to combine all CSVs in a folder into one CSV
How to output the output result of the Linux man command to a file
[Django] How to read variables / constants defined in an external file
[Python] How to store a csv file as one-dimensional array data
How to make a deadman's switch
How to create a Dockerfile (basic)
[Blender] How to make a Blender plugin
Convert a string to an image
How to delete a Docker container
How to create a CSV dummy file containing Japanese using Faker
How to get a job as an engineer from your 30s
How to make a crawler --Basic
[ROS2] How to play a bag file with python format launch
How to turn ugly code into plain code that isn't a beautiful object-oriented design with Python refactoring
"Cython" tutorial to make Python explosive: How to parse an Enum class defined in C ++ code into a Python Enum.
How to specify a .ui file in the dialog / widget GUI in PySide
How to put a line number at the beginning of a CSV file
[Python] How to read a csv file (read_csv method of pandas module)
Use BeautifulSoup to extract a link containing a string from an HTML file