Skip to content
Home

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.

  1. Download python (version 3.8) from here. Make sure it should be the windows version.

  2. 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
  3. Install python

    # Download path maybe different
    wine /home/dev/Downloads/python-3.8.10-amd64.exe>
  4. Find out where pip or python is installed. We will create an virutal environment. For this we require, where python is created.

    find ~/.wine -name python.exe
    # /home/dev/.wine/drive_c/users/dev/Local Settings/Application Data/Programs/Python/Python38/python.exe
  5. Setup an virtual environment

    # create an venv
    python -m venv ./dist/converter
    wine "/home/dev/.wine/drive_c/users/dev/Local Settings/Application Data/Programs/Python/Python38/python.exe" -m venv /app/dist/converter
  6. The pip.exe is located under /app/dist/converter/Scripts/ and install the packages

    wine "/home/dev/app/dist/converter/Scripts/pip.exe" install openpyxl xlrd pyinstaller
  7. Convert the script into EXE file

    wine "/home/dev/app/dist/converter/Scripts/pyinstaller.exe" --onefile /app/excel_converter.py

    The output is generated in the dist folder.

  8. 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.xlsx
    Folder 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