Create an exe file that works in a Windows environment without Python with PyInstaller

A memo that I made a trial and error to run the created machine learning program in a Windows environment where Python is not installed.

The environment is as follows

Basic usage of PyInstaller

Installation

$ pip install pyinstaller

exe file creation

$ pyinstaller main.py --onefile

With this, the Python execution environment and related libraries are also combined into one exe file.

However, if there is a dependency that PyInstaller cannot resolve, the created exe file will throw an error.

In that case, build without using the --onefile option and solve the following two points one by one.

When you can not exe properly with Pyinstaller

I get * .so not found

You are not importing the binaries of the external libraries you depend on. It seems that PyInstaller automatically imports what is directly referenced, but it seems that it does not consider what is indirectly referenced. You can specify to explicitly include it with the --add-binary option. Note that the method of specifying Path is special

--add-binary "<Relative path of the source SO file>;<Run-time relative path of the destination directory>"

The way to specify the path should have been described correctly, but it is not placed properly

The cache seemed to be bad, so with the --clean option, it was placed as specified.

I get a ModuleNotFoundError

Similarly, the external library is not imported here either. It seems that the one that is indirectly referenced in the Python library system corresponds. You can specify it with hidden-import when executing pyinstaller. While adding, repeat exe conversion and continue until ModuleNotFoundError disappears.

For example, when I made a text classification program using tensorflow, it looked like this.

$ pyinstaller main.py ¥
--hidden-import=tensorflow.python.keras.engine.base_layer_v1 ¥
--hidden-import=tensorflow.python.ops.while_v2 ¥
--hidden-import=tensorflow.python.ops.numpy_ops

Final pyinstaller runtime options

For example, it looks like this.

$ pyinstaller main.py --onefile -y --clean \
   --hidden-import=tensorflow.python.keras.engine.base_layer_v1 \
   --hidden-import=tensorflow.python.ops.while_v2 \
   --hidden-import=tensorflow.python.ops.numpy_ops \
   --add-binary "../../../../AppData/Local/Programs/Python/Python36/lib/site-packages/tensorflow/lite/experimental/microfrontend/python/ops/_audio_microfrontend_op.so;tensorflow/lite/experimental/microfrontend/python/ops" 

Recommended Posts

Create an exe file that works in a Windows environment without Python with PyInstaller
Create an executable file (EXE) by PyInstaller in a hybrid environment (Nimporter) of Python + Nim
[Python Kivy] How to create an exe file with pyinstaller
Create a virtual environment with conda in Python
Creating an exe file with Python PyInstaller: PC freezes in parallel processing
Let's create a script that registers with Ideone.com in Python.
Create a virtual environment with Python!
Create an Excel file with Python3
Create a binary file in Python
Create a Python execution environment for Windows with VScode + Remote WSL
How to create a heatmap with an arbitrary domain in Python
Quickly create an excel file with Python #python
Building a Python 3.6 environment with Windows + PowerShell
[GPS] Create a kml file in Python
Steps to create a Python virtual environment with VS Code on Windows
Create a Vim + Python test environment in 1 minute
Create a GIF file using Pillow in Python
Create an app that guesses students with python
Building an environment that uses Python in Eclipse
Create an executable file in a scripting language
How to create a JSON file in Python
Create an environment of 64bit Windows + python 2.7 + MeCab 0.996
Create a page that loads infinitely with python
Create a python3 build environment with Sublime Text3
Develop Windows apps with Python 3 + Tkinter (exe file)
Create an image with characters in python (Japanese)
GUI (WxPython) executable file (pyInstaller) [Windows] in Python3
Work in a virtual environment with Python virtualenv.
Create a new page in confluence with Python
Create a Photoshop format file (.psd) with python
Create a MIDI file in Python using pretty_midi
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Create a Python virtual development environment on Windows
A script that retrieves tweets with Python, saves them in an external file, and performs morphological analysis.
Create a Python image in Django without a dummy image file and test the image upload
Create an elliptical scatter plot in Python without using a multivariate normal distribution
Create a Python environment
Prepare a development environment that is portable and easy to duplicate without polluting the environment with Python embeddable (Windows)
Create a comfortable Python 3 (Anaconda) development environment on windows
Create a python development environment with vagrant + ansible + fabric
In Python, create a decorator that dynamically accepts arguments Create a decorator
Create an OpenAI Gym environment with bash on Windows 10
Run a Python file with relative import in PyCharm
[Python] Create a Tkinter program distribution file with cx_Freeze
Create a decent shell and python environment on Windows
Create a fake Minecraft server in Python with Quarry
Create a 2d CAD file ".dxf" with python [ezdxf]
I tried to build an environment with WSL + Ubuntu + VS Code in a Windows environment
A story about how Windows 10 users created an environment to use OpenCV3 with Python 3.5
File overwrite confirmation with option that takes a file object as an argument with Python argparse
Until you create a machine learning environment with Python on Windows 7 and run it
Create an environment with virtualenv
Create a function in Python
Create a dictionary in Python
Create a directory with python
Try running python in a Django environment created with pipenv
[Python] Create a file & folder path specification screen with tkinter
Create a local scope in Python without polluting the namespace
Create a list in Python with all followers on twitter
Create a child account for connect with Stripe in Python
Create code that outputs "A and pretending B" in python