Convert python script to exe files in Ubuntu
Introduction
Converting a python script to an executable has meaningful benefits.
- Python does not need to installed in the client machine
- Easy distribution
- Useful in automation tasks
The rest of the article is based on the sample script.
Environment tested on
Instructions
Perform the following commands in a terminal opened in Ubuntu.
-
Download python (version 3.8) from here. Make sure it should be the windows version.
-
Install
wine
. Wine is a compatibility layer capable of running Windows applications on several POSIX-compliant operating systems, such as Linux, macOS, & BSD.sudo apt install wine winetricks -
Install python
# Download path maybe differentwine /home/dev/Downloads/python-3.8.10-amd64.exe> -
Find out where
pip
orpython
is installed. We will create an virutal environment. For this we require, wherepython
is created.find ~/.wine -name python.exe# /home/dev/.wine/drive_c/users/dev/Local Settings/Application Data/Programs/Python/Python38/python.exe -
Setup an virtual environment
# create an venvpython -m venv ./dist/converterwine "/home/dev/.wine/drive_c/users/dev/Local Settings/Application Data/Programs/Python/Python38/python.exe" -m venv /app/dist/converter -
The
pip.exe
is located under /app/dist/converter/Scripts/ and install the packageswine "/home/dev/app/dist/converter/Scripts/pip.exe" install openpyxl xlrd pyinstaller -
Convert the script into EXE file
wine "/home/dev/app/dist/converter/Scripts/pyinstaller.exe" --onefile /app/excel_converter.pyThe output is generated in the
dist
folder. -
Copy and Test the EXE file in a windows machine. Open Command prompt or Powershell
Single excel file execution
excel_converter.exe test.xls# C:\Users\basdemo\Desktop\trash\dist>excel_converter.exe test-file.xls# Converted: test-file.xls to test-file.xlsxFolder execution
excel_converter.exe Folder# C:\Users\basdemo\Desktop\trash\dist>excel_converter.exe Folder# Converted: Folder\test-file - Kopie (2).xls to Folder\test-file - Kopie (2).xlsx# Converted: Folder\test-file - Kopie (3).xls to Folder\test-file - Kopie (3).xlsx# Converted: Folder\test-file - Kopie (4).xls to Folder\test-file - Kopie (4).xlsx# Converted: Folder\test-file - Kopie (5).xls to Folder\test-file - Kopie (5).xlsx# Converted: Folder\test-file - Kopie.xls to Folder\test-file - Kopie.xlsx# Converted: Folder\test-file.xls to Folder\test-file.xlsx