PyInstaller memorandum Convert Python [.py] to [.exe] with 2 lines

Introduction

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.

quick start

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

Option list

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)

Generation options

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

OS-specific options

option function
-w, --windowed, --noconsole Hide the console
-i, --icon Program icon[.ico extension]To set

General options

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

Other options

option function
--exclude Ignore the specified module or package. Can be used multiple times

Option usage example

pyinstaller yourprogram.py --exclude numpy,-v, -i data.icon

Run PyInstaller from Python code

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'),
])

Summary

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

PyInstaller memorandum Convert Python [.py] to [.exe] with 2 lines
[Python Kivy] How to create an exe file with pyinstaller
Convert memo at once with Python 2to3
How to convert Python to an exe file
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Convert FX 1-minute data to 5-minute data with Python
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
Python script written in PyTorch is converted to exe with PyInstaller
Sample to convert image to Wavelet with Python
Convert the program that imports Google's client library to .exe with pyinstaller
Convert PDF to image (JPEG / PNG) with Python
Convert PDFs to images in bulk with Python
Convert svg file to png / ico with Python
Convert Windows epoch values to date with python
Convert strings to character-by-character list format with python
Convert 202003 to 2020-03 with pandas
Convert jupyter to py
Convert jupyter notebook .ipynb files to python executable .py files
How to convert / restore a string with [] in python
Convert the image in .zip to PDF with Python
How to convert JSON file to CSV file with Python Pandas
[Python] What to do when No module named'pyproj.datadir' appears when Exe is done with PyInstaller
Connect to BigQuery with Python
[python] Convert date to string
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Connect to Wikipedia with Python
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
[Python3] Dijkstra's algorithm with 14 lines
Switch python to 2.7 with alternatives
Write to csv with Python
Convert python 3.x code to python 2.x
How to convert an array to a dictionary with Python [Application]
Convert images to sepia toning with PIL (Python Imaging Library)
Convert video to black and white with ffmpeg + python + opencv
Convert .ipynb to .html (with BatchFile)
Convert to a string while outputting standard output with Python subprocess
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
Python: How to use async with
I tried to convert a Python file to EXE (Recursion error supported)
Link to get started with python
Convert markdown to PDF in Python
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Nice to meet you with python
Convert files written in python etc. to pdf with syntax highlighting
Try to operate Facebook with Python
Output to csv file with Python
Workflow to convert formula (image) to python
How to get the directory where the EXE built with Pyinstaller exists
MP3 to WAV conversion with Python
Convert sentences to vectors with gensim
To do tail recursion with Python2
How to get started with Python
Python> list> Convert double list to single list
What to do with PYTHON release?
Unable to install Python with pyenv
How to use FTP with Python