Standalone Python Executable

Python memang bukan bahasa pemrograman yang compiled based language, tapi kita bisa membuat executable file menggunakan package pyinstaller. Namun untuk membuat executable diharuskan menggunakan sistem operasi yang dituju.

Semisalkan ingin membuat executable dengan ekstensi .exe harus menggunakan sistem operasi Windows. Karena jika melakukan development di Linux tapi ingin membuat file executable ke Windows itu tidak bisa.

Siapin file Python sederhana isi dengan Hello World:

print('Hello World!')

Kalau misalkan dirunning akan tampil output Hello World:

PS D:\standalone-python> python3 script.py
Hello World!

Setelah itu install pip package pyinstaller:

PS D:\standalone-python> pip3 install pyinstaller
Requirement already satisfied: pyinstaller in c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages (5.3)
Requirement already satisfied: pyinstaller-hooks-contrib>=2021.4 in c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages (from pyinstaller) (2022.10)
Requirement already satisfied: altgraph in c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages (from pyinstaller) (0.17.2)
Requirement already satisfied: pywin32-ctypes>=0.2.0 in c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages (from pyinstaller) (0.2.0)
Requirement already satisfied: setuptools in c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages (from pyinstaller) (63.2.0)
Requirement already satisfied: pefile>=2022.5.30 in c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages (from pyinstaller) (2022.5.30)
Requirement already satisfied: future in c:\users\lenovo\appdata\local\programs\python\python310\lib\site-packages (from pefile>=2022.5.30->pyinstaller) (0.18.2)

Already satisfied karena saya udah pernah install.

Setelah itu coba dibuat jadi executable file dengan perintah:

PS D:\standalone-python> pyinstaller namafile.py
PS D:\standalone-python> pyinstaller script.py
269 INFO: PyInstaller: 5.3
269 INFO: Python: 3.10.7
278 INFO: Platform: Windows-10-10.0.22000-SP0
279 INFO: wrote D:\standalone-python\script.spec
281 INFO: UPX is not available.
284 INFO: Extending PYTHONPATH with paths
['D:\\standalone-python']
582 INFO: checking Analysis
582 INFO: Building Analysis because Analysis-00.toc is non existent
582 INFO: Initializing module dependency graph...
584 INFO: Caching module graph hooks...
607 INFO: Analyzing base_library.zip ...
4564 INFO: Processing pre-find module path hook distutils from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
4567 INFO: distutils: retargeting to non-venv dir 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib'
5504 INFO: Caching module dependency graph...
5628 INFO: running Analysis Analysis-00.toc
5632 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\python.exe
5816 INFO: Analyzing D:\standalone-python\script.py
5817 INFO: Processing module hooks...
5818 INFO: Loading module hook 'hook-difflib.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5819 INFO: Loading module hook 'hook-distutils.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5820 INFO: Loading module hook 'hook-distutils.util.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5822 INFO: Loading module hook 'hook-encodings.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
6116 INFO: Loading module hook 'hook-heapq.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
6119 INFO: Loading module hook 'hook-lib2to3.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
6157 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
6160 INFO: Loading module hook 'hook-pickle.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
6163 INFO: Loading module hook 'hook-platform.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
6165 INFO: Loading module hook 'hook-sysconfig.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
6167 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
6167 INFO: Loading module hook 'hook-xml.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
6207 INFO: Loading module hook 'hook-_tkinter.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
6347 INFO: checking Tree
6347 INFO: Building Tree because Tree-00.toc is non existent
6347 INFO: Building Tree Tree-00.toc
6420 INFO: checking Tree
6421 INFO: Building Tree because Tree-01.toc is non existent
6421 INFO: Building Tree Tree-01.toc
6504 INFO: checking Tree
6504 INFO: Building Tree because Tree-02.toc is non existent
6504 INFO: Building Tree Tree-02.toc
6520 INFO: Looking for ctypes DLLs
6533 INFO: Analyzing run-time hooks ...
6535 INFO: Including run-time hook 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
6537 INFO: Including run-time hook 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_subprocess.py'
6541 INFO: Looking for dynamic libraries
7280 INFO: Looking for eggs
7280 INFO: Using Python library C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\python310.dll
7281 INFO: Found binding redirects:
[]
7283 INFO: Warnings written to D:\standalone-python\build\script\warn-script.txt
7313 INFO: Graph cross-reference written to D:\standalone-python\build\script\xref-script.html
7333 INFO: checking PYZ
7333 INFO: Building PYZ because PYZ-00.toc is non existent
7333 INFO: Building PYZ (ZlibArchive) D:\standalone-python\build\script\PYZ-00.pyz
7552 INFO: Building PYZ (ZlibArchive) D:\standalone-python\build\script\PYZ-00.pyz completed successfully.
7559 INFO: checking PKG
7559 INFO: Building PKG because PKG-00.toc is non existent
7559 INFO: Building PKG (CArchive) script.pkg
7577 INFO: Building PKG (CArchive) script.pkg completed successfully.
7579 INFO: Bootloader C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
7579 INFO: checking EXE
7579 INFO: Building EXE because EXE-00.toc is non existent
7579 INFO: Building EXE from EXE-00.toc
7580 INFO: Copying bootloader EXE to D:\standalone-python\build\script\script.exe.notanexecutable
7676 INFO: Copying icon to EXE
7676 INFO: Copying icons from ['C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
7737 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
7737 INFO: Writing RT_ICON 1 resource with 3752 bytes
7738 INFO: Writing RT_ICON 2 resource with 2216 bytes
7738 INFO: Writing RT_ICON 3 resource with 1384 bytes
7738 INFO: Writing RT_ICON 4 resource with 37019 bytes
7738 INFO: Writing RT_ICON 5 resource with 9640 bytes
7738 INFO: Writing RT_ICON 6 resource with 4264 bytes
7739 INFO: Writing RT_ICON 7 resource with 1128 bytes
7740 INFO: Copying 0 resources to EXE
7741 INFO: Embedding manifest in EXE
7741 INFO: Updating manifest in D:\standalone-python\build\script\script.exe.notanexecutable
7802 INFO: Updating resource type 24 name 1 language 0
7804 INFO: Appending PKG archive to EXE
7810 INFO: Fixing EXE headers
8053 INFO: Building EXE from EXE-00.toc completed successfully.
8055 INFO: checking COLLECT
8056 INFO: Building COLLECT because COLLECT-00.toc is non existent
8056 INFO: Building COLLECT COLLECT-00.toc
8324 INFO: Building COLLECT COLLECT-00.toc completed successfully.

Di atas adalah output dari perintah pyinstallernya. Completed successsfully berarti udah selesai. File executablenya itu terletak di dalam folder dist/script di dalamnya ada executable script.exe dan itu adalah hasil executable dari file Python tersebut.

PS D:\standalone-python> tree
Folder PATH listing
Volume serial number is 0ECC-C7B4
D:.
├───build
   └───script
       └───localpycs
└───dist
    └───script
PS D:\standalone-python> .\dist\script\script.exe
Hello World!

Okay everything is fine kita udah bisa membuat executable file dari file Python ke .exe file. Tapi ada satu masalah.

Kalau semisalkan file executable tersebut diberikan ke laptop lain dengan sistem operasi yang sama yaitu Windows, executable file tersebut tidak akan bisa running karena missing python310.dll file.

PS D:\> .\script.exe
Error loading Python DLL 'D:\python310.dll'.
LoadLibrary: The specified module could not be found.

Karena di dalam folder dist/script terdapat banyak library file yang khusus untuk file script.exe itu. Kalau misalkan itu tidak di satu folder dengan file script.exe otomatis file executablenya tidak bisa running.

PS D:\standalone-python> tree /F .\dist\script\
Folder PATH listing
Volume serial number is 0ECC-C7B4
D:\STANDALONE-PYTHON\DIST\SCRIPT
    api-ms-win-core-console-l1-1-0.dll
    api-ms-win-core-datetime-l1-1-0.dll
    api-ms-win-core-debug-l1-1-0.dll
    api-ms-win-core-errorhandling-l1-1-0.dll
    api-ms-win-core-fibers-l1-1-0.dll
    api-ms-win-core-file-l1-1-0.dll
    api-ms-win-core-file-l1-2-0.dll
    api-ms-win-core-file-l2-1-0.dll
    api-ms-win-core-handle-l1-1-0.dll
    api-ms-win-core-heap-l1-1-0.dll
    api-ms-win-core-interlocked-l1-1-0.dll
    api-ms-win-core-libraryloader-l1-1-0.dll
    api-ms-win-core-localization-l1-2-0.dll
    api-ms-win-core-memory-l1-1-0.dll
    api-ms-win-core-namedpipe-l1-1-0.dll
    api-ms-win-core-processenvironment-l1-1-0.dll
    api-ms-win-core-processthreads-l1-1-0.dll
    api-ms-win-core-processthreads-l1-1-1.dll
    api-ms-win-core-profile-l1-1-0.dll
    api-ms-win-core-rtlsupport-l1-1-0.dll
    api-ms-win-core-string-l1-1-0.dll
    api-ms-win-core-synch-l1-1-0.dll
    api-ms-win-core-synch-l1-2-0.dll
    api-ms-win-core-sysinfo-l1-1-0.dll
    api-ms-win-core-timezone-l1-1-0.dll
    api-ms-win-core-util-l1-1-0.dll
    api-ms-win-crt-conio-l1-1-0.dll
    api-ms-win-crt-convert-l1-1-0.dll
    api-ms-win-crt-environment-l1-1-0.dll
    api-ms-win-crt-filesystem-l1-1-0.dll
    api-ms-win-crt-heap-l1-1-0.dll
    api-ms-win-crt-locale-l1-1-0.dll
    api-ms-win-crt-math-l1-1-0.dll
    api-ms-win-crt-process-l1-1-0.dll
    api-ms-win-crt-runtime-l1-1-0.dll
    api-ms-win-crt-stdio-l1-1-0.dll
    api-ms-win-crt-string-l1-1-0.dll
    api-ms-win-crt-time-l1-1-0.dll
    api-ms-win-crt-utility-l1-1-0.dll
    base_library.zip
    libcrypto-1_1.dll
    libssl-1_1.dll
    python310.dll
    script.exe
    select.pyd
    ucrtbase.dll
    unicodedata.pyd
    VCRUNTIME140.dll
    _bz2.pyd
    _decimal.pyd
    _hashlib.pyd
    _lzma.pyd
    _socket.pyd
    _ssl.pyd

No subfolders exist

Ini nih repot lagi karena harus install Python ke target laptop yang mau running executable filenya.

Tapi tidak perlu install lagi Python ke target laptop yang mau running executablenya, ada cara yang lebih mudah jadi file executablenya itu 100% standalone bisa dipakai tanpa harus install Python di machine target yang mau running executable filenya.

Di perintah pyinstaller tadi tambahin flag -F --add-binary 'path/to/python.dll'

Contoh:

Perintah di bawah ini akan memasukkan library file Python ke executablenya.

PS D:\standalone-python> pyinstaller -F --add-binary "C:\Users\<username>\AppData\Local\Programs\Python\Python310\python310.dll;." script.py

Perlu diperhatikan untuk <username> sesuaikan dengan nama username masing-masing dan versi file pythonnya juga bisa jadi berbeda-beda sesuai dengan versi berapa yang kalian install di mesin kalian.

269 INFO: PyInstaller: 5.3
269 INFO: Python: 3.10.7
279 INFO: Platform: Windows-10-10.0.22000-SP0
279 INFO: wrote D:\standlone-python\script.spec
279 INFO: UPX is not available.
287 INFO: Extending PYTHONPATH with paths
['D:\\standlone-python']
563 INFO: checking Analysis
563 INFO: Building Analysis because Analysis-00.toc is non existent
563 INFO: Initializing module dependency graph...
569 INFO: Caching module graph hooks...
580 INFO: Analyzing base_library.zip ...
4299 INFO: Processing pre-find module path hook distutils from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
4299 INFO: distutils: retargeting to non-venv dir 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib'
5219 INFO: Caching module dependency graph...
5344 INFO: running Analysis Analysis-00.toc
5344 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\python.exe
5529 INFO: Analyzing D:\standlone-python\script.py
5529 INFO: Processing module hooks...
5529 INFO: Loading module hook 'hook-difflib.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5534 INFO: Loading module hook 'hook-distutils.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5534 INFO: Loading module hook 'hook-distutils.util.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5534 INFO: Loading module hook 'hook-encodings.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5800 INFO: Loading module hook 'hook-heapq.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5800 INFO: Loading module hook 'hook-lib2to3.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5829 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5834 INFO: Loading module hook 'hook-pickle.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5834 INFO: Loading module hook 'hook-platform.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5834 INFO: Loading module hook 'hook-sysconfig.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5839 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5839 INFO: Loading module hook 'hook-xml.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
5884 INFO: Loading module hook 'hook-_tkinter.py' from 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks'...
6014 INFO: checking Tree
6019 INFO: Building Tree because Tree-00.toc is non existent
6019 INFO: Building Tree Tree-00.toc
6069 INFO: checking Tree
6069 INFO: Building Tree because Tree-01.toc is non existent
6069 INFO: Building Tree Tree-01.toc
6140 INFO: checking Tree
6144 INFO: Building Tree because Tree-02.toc is non existent
6144 INFO: Building Tree Tree-02.toc
6156 INFO: Looking for ctypes DLLs
6164 INFO: Analyzing run-time hooks ...
6169 INFO: Including run-time hook 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
6172 INFO: Including run-time hook 'C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_subprocess.py'
6172 INFO: Looking for dynamic libraries
6852 INFO: Looking for eggs
6852 INFO: Using Python library C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\python310.dll
6852 INFO: Found binding redirects:
[]
6852 INFO: Warnings written to D:\standlone-python\build\script\warn-script.txt
6884 INFO: Graph cross-reference written to D:\standlone-python\build\script\xref-script.html
6889 INFO: Appending 'binaries' from .spec
6904 INFO: checking PYZ
6904 INFO: Building PYZ because PYZ-00.toc is non existent
6904 INFO: Building PYZ (ZlibArchive) D:\standlone-python\build\script\PYZ-00.pyz
7119 INFO: Building PYZ (ZlibArchive) D:\standlone-python\build\script\PYZ-00.pyz completed successfully.
7133 INFO: checking PKG
7133 INFO: Building PKG because PKG-00.toc is non existent
7133 INFO: Building PKG (CArchive) script.pkg
8733 INFO: Building PKG (CArchive) script.pkg completed successfully.
8733 INFO: Bootloader C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
8739 INFO: checking EXE
8739 INFO: Building EXE because EXE-00.toc is non existent
8739 INFO: Building EXE from EXE-00.toc
8739 INFO: Copying bootloader EXE to D:\standlone-python\dist\script.exe.notanexecutable
8809 INFO: Copying icon to EXE
8809 INFO: Copying icons from ['C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
8874 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
8874 INFO: Writing RT_ICON 1 resource with 3752 bytes
8874 INFO: Writing RT_ICON 2 resource with 2216 bytes
8874 INFO: Writing RT_ICON 3 resource with 1384 bytes
8874 INFO: Writing RT_ICON 4 resource with 37019 bytes
8874 INFO: Writing RT_ICON 5 resource with 9640 bytes
8874 INFO: Writing RT_ICON 6 resource with 4264 bytes
8874 INFO: Writing RT_ICON 7 resource with 1128 bytes
8874 INFO: Copying 0 resources to EXE
8874 INFO: Embedding manifest in EXE
8879 INFO: Updating manifest in D:\standlone-python\dist\script.exe.notanexecutable
8939 INFO: Updating resource type 24 name 1 language 0
8939 INFO: Appending PKG archive to EXE
8949 INFO: Fixing EXE headers
10320 INFO: Building EXE from EXE-00.toc completed successfully.

Nah perlu diingat lagi, path library .dll Python dan username Windows itu berbeda-beda ya tergantung sistem operasi yang digunakan. Kebetulan untuk Windows pathnya disitu.

Setelah itu file executablenya udah 100% bisa running tuh di mana aja tanpa butuh library file lagi ataupun install Python ke machine yang mau running executable filenya.

Referensi: