How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script

Introduction

When you want to pass a Python script to another person, it is troublesome to build an environment on the other person's PC. I would like to make it an executable file if possible. Pyinstaller and Nuitka are useful modules to achieve this. The latter is said to have the characteristics that the size of the executable file is small and the execution speed is fast after converting to C and then compiling.

After compiling Nuitka, it can be troublesome to run it and fix it if you get an Import Error (also Pyinstaller). Therefore, using the hinted-compilation of NUITKA-Utilities, run the Python script once to understand the module used, and then compile it to reduce failures. It was very easy to use and convenient, so I would like to introduce it with a comparison with Pyinstaller.

reference

I found out about NUITKA-Utilities here. Please refer to here for how to use scripts other than hinted-compilation. Qiita: Easy Python compilation with NUITKA-Utilities

1. Preparation of NUITKA-Utilities

Download NUITKA-Utilities. GitHub: NUITKA-Utilities Install nuitka, pysimplegui, and MinGW in advance. In the case of Anaconda environment, it is as follows.

conda install -c conda-forge nuitka
conda install -c conda-forge pysimplegui

Installing MinGW is a bit tricky and should be matched with the installed Python architecture. Type Python at the command prompt to confirm.

C:\>python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, 
Inc. on win32

Install MinGW by referring to Let's Programming: Downloading and installing MinGW-w64. In the above example, Python is 64bit, so change the MinGW installer Architecture to x86_x64. MinGWinstaller.png

Make sure your PATH is correct.

C:\>gcc --version
gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

If you do not want to set the PATH because there is another version originally, you can execute mingw-w64.bat where MinGW is installed.

2. How to use NUITKA-Utilities hinted-compilation

For the sake of explanation, name the script file you want to compile as yourscript.py.

  1. Go to the NUITKA-Utilities \ hinted-compilation directory and run the script as shown below. The script will be executed and the used module will be written to the json file.

    python get-hints.py yourscript.py
    
  2. Then run the script as follows: At this time, put yourscript.py and the previous json file in the same hierarchy.

    python nuitka-hints.py yourscript.py
    

With just this, you can create an executable file from a Python script file. If you want to combine the files into one, use onefile-maker-windows.py.

3. Comparison of file size, execution speed and startup speed

code

I wrote the code based on the problem that appeared in SPI3. Using this as an example, measure the execution speed and startup speed.

import numpy as np
import time

def coin_prob(N=1000000):
    """
Throw 1 10-yen coin, 2 5-yen coins, 5 1-yen coins,
Add the amount of coins on the table.
What is the probability of becoming 15 yen?
    """
    coins = np.array([10,5,5,1,1,1,1,1])
    count=0
    for _ in range(N):
        heads_or_tails = np.random.randint(0,2,len(coins)) #0 or 1
        if np.sum(coins*heads_or_tails) == 15:
            count += 1
    print("The probability is{}".format(count/N))

def main():
    """
Find the mean time and standard deviation of 10 times.
    """
    elapsed_times = []
    for _ in range(10):
        start = time.time()
        coin_prob()
        end = time.time()
        elapsed_times.append(end-start)

    ave_time = np.average(elapsed_times)
    std_time = np.std(elapsed_times)
    print("processing time={0}±{1}".format(ave_time,std_time))

if __name__ == "__main__":
    start_all = time.time()
    main()
    end_all = time.time()
    print("Total elapsed time{}".format(end_all-start_all))

Neither Pyinstaller nor Nuitka have the option to combine them into one file.

As an aside, both Pyinstaller and Nuitka didn't go smoothly and suffered a little. Pyinstaller solved it by referring to this article. Avoid run-time error ModuleNotFoundError for executables generated from Python code using Qiita: Pyinstaller Nuitka did not go well with mkl and solved it here. Notes on Nuupan: What to do if mkl fails to load under the conda environment

File size

In the beginning, Nuitka said the file size was small, but it wasn't. I hope it will continue to be improved and will not import extra modules.

Original Pyinstaller Nuitka
1.02KB 569MB 623MB

Execution speed

The average and standard deviation of 10 coin-throwing trials are shown as the processing time for one set. It's not the point, but I also included Numba jit for comparison. If you want speedup, you should use this.

Original Pyinstaller Nuitka Numba jit
Time s 18.44±0.54 18.30±0.26 4.275±0.076 0.2299±0.1281
Gain 1.000 1.008 4.314 80.21

Startup speed

The time from the start to the end of the program was measured with timeit.exe of Windows Server 2003 Resource Kit Tools, and the difference from the processing time of the main function inside the program was measured three times.

Original Pyinstaller Nuitka
Time ms 213.3±2.8 235.8±2.2 88.39±1.72
Gain 1.000 0.905 2.414

Summary

Except for the file size, it outperformed Pyinstaller in terms of execution speed and startup speed. It is recommended because it has fewer errors and is easier than Pyinstaller.

Recommended Posts

How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
Python script to create a JSON file from a CSV file
How to create a JSON file in Python
How to create a kubernetes pod from python code
I made a package to create an executable file from Hy source code
How to run a Python program from within a shell script
[Python Kivy] How to create an exe file with pyinstaller
[Python] How to output a pandas table to an excel file
How to create a config file
How to create an instance of a particular class from dict using __new__ () in python
Create a shell script to run the python file multiple times
How to create a heatmap with an arbitrary domain in Python
How to create a clone from Github
How to convert Python to an exe file
Create a deb file from a python package
How to create a repository from media
Use Django from a local Python script
Script to create a Mac dictionary file
How to run a Maya Python script
Use BeautifulSoup to extract a link containing a string from an HTML file
Edit Excel from Python to create a PivotTable
How to read a CSV file with Python 2/3
How to create a Python virtual environment (venv)
How to open a web browser from python
Study from Python Hour7: How to use classes
Create an executable file in a scripting language
Create a New Todoist Task from Python Script
How to generate a Python object from JSON
[Python] How to easily drop a child process started by multiprocess from another process
How to create an article from the command line
[Blender x Python] How to create an original object
Backtrader How to import an indicator from another file
A memorandum to run a python script in a bat file
How to create a simple TCP server / client script
[Python] How to create a 2D histogram with Matplotlib
[Python] How to call a c function from python (ctypes)
A story about how Windows 10 users created an environment to use OpenCV3 with Python 3.5
How to create a radial profile from astronomical images (Chandra, XMM etc.) using python
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to slice a block multiple array from a multiple array in Python
How to convert an array to a dictionary with Python [Application]
A Python script that saves a clipboard (GTK) image to a file.
How to use the __call__ method in a Python class
How to launch AWS Batch from a python client app
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)
[Python Kivy] How to create a simple pop up window
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
Let's use Watson from Python! --How to use Developer Cloud Python SDK