Overview

In this guide, we'll walk you through the process, tailored to each specific platform. Whether you're running Windows, Mac, or Linux, you'll find the steps you need to create a standalone executable version of the code. Let's get started by exploring the sample Python code for the conversion process.

Sample Code for Conversion

We have a program designed to convert measurements from feet and inches to meters, and our goal is to transform it into a standalone executable. However, the process varies depending on the operating system: Windows, Mac, and Linux.

If you intend to create an executable for Windows, you must have access to a Windows computer. The same principle applies to Mac and Linux systems as well.

With this in mind, let's begin the process of creating the standalone executable on a Windows platform.

So, below are the sample code for conversion:

#file name: meter_converter.py
import functions
import PySimpleGUI as sg

label1 = sg.Text("Enter feet")
input1 = sg.Input(key="this_feet")

label2 = sg.Text("Enter inches")
input2 = sg.Input(key="this_inches")

button_convert = sg.Button("Convert")
output_meter = sg.Text(key="output", text_color="green")

convertor_layout = [[label1, input1], [label2, input2], [button_convert, output_meter]]

window = sg.Window("Convertor", layout=convertor_layout)

while True:
    event, value = window.read()
    print("1.event:", event)
    print("2.value: ", value)

    if event == sg.WINDOW_CLOSED:  # Check if the window was closed
        break

    cal_feet = int(value["this_feet"])
    cal_inches = int(value["this_inches"])

    cal_meters = functions.convert_to_meter(cal_feet, cal_inches)

    window["output"].update(value=str(cal_meters) + " meters")

window.close()

#file name: functions.py
def convert_to_meter(ft, inch):
    """convert answer to meter
    receiving two arguments (feet, inches)
    return 1 value (meter) """
    meter1 = ft * 0.3048
    meter2 = inch * 0.0254
    meter = meter1 + meter2
    return meter

if __name__ == "__main__":
    dev_input_ft = int(input("Enter ft: "))
    dev_input_inch = int(input("Enter inch: "))
    dev_ans = convert_to_meter(dev_input_ft, dev_input_inch)
    print("Answer in meter: ", dev_ans)

There will be two python file, meter_converter.py and functions.py. Make sure both files located in the same folder. Run the code in your system first to ensure no errors occur before converting it to standalone executable.

Converting To Standalone Executable

To open PyCharm and access the terminal: Launch PyCharm. Then, navigate to the terminal.

1. Once you're in the terminal, you should observe something similar to the following:

Converting To Standalone Executable

2. If you encounter an error, you can rectify it by executing the following code.

set-executionpolicy remotesigned -scope currentuser

3. After entering the code, close the terminal application.

4. Reopen the terminal to ensure that the error message has been eliminated.

Generating the Executable File (Windows)

Installing PyInstaller: A Third-Party Tool

Option 1: Using the Terminal

To install the necessary third-party tool, pyinstaller, follow these steps:

1. Open your terminal.

2. Type in the following code and press Enter:

pip install pyinstaller 

Option 2: Using PyCharm

If you are using PyCharm, you can also install pyinstaller through the following method:

1. Open PyCharm.

2. Navigate to the "Package" tab.

3. Search for 'pyinstaller' in the package search bar.

4. Once you find it, click on it and install it.

Generating the Executable File (Windows)

To generate the executable file for your Python script, follow these steps in your terminal:

1. Open the terminal.

2. Type in the following command:

pyinstaller --onefile --windowed --clean meter_converter.py 

Here's what each option does:

--onefile: This option creates a single executable file.

--windowed: It prevents the command line from being displayed when running the executable.

--clean: Use this option to overwrite any previously generated files if necessary.

Generating the Executable File with Platypus (Mac)

Installing Platypus: A Third-Party Tool

To install the necessary third-party tool, Platypus, follow these steps:

1. Open your web browser on your Mac.

2. Visit the Platypus website at https://sveinbjorn.org/platypus.

3. Download the Platypus application for macOS from the official website.

4. Install Platypus by following the installation instructions provided on the website.

Generating the Executable File with Platypus (Mac)

To generate the executable file for your Python script using Platypus on your Mac, follow these steps:

1. Open Platypus.

2. Click on "Create New Script."

3. In the "Script Name" field, enter a name for your application (e.g., "Meter Converter").

4. In the "Interface" section, select "None" to create a GUI-based executable without a command-line interface.

5. In the "Script" section, select "Python" as the script type.

6. Click on the "Choose Script" button and navigate to your Python script file (meter_converter.py).

7. Configure any additional settings or options you require for your executable.

8. Click the "Create" button to generate the executable.

9. Platypus will create the executable application in the location you specified.

Now, you have successfully generated an executable file for your Python script using Platypus on your Mac. This executable will have a graphical user interface (GUI) and can be run without displaying a command-line interface.

Generating the Executable File (Linux)

Installing PyInstaller: A Third-Party Tool

Option 1: Using the Terminal (Linux)

To install the necessary third-party tool, pyinstaller, follow these steps:

1. Open your terminal.

2. Type in the following code and press Enter:

pip install pyinstaller 

Option 2: Using PyCharm

If you are using PyCharm, you can also install pyinstaller through the following method:

1. Open PyCharm.

2. Navigate to the "Package" tab.

3. Search for 'pyinstaller' in the package search bar.

4. Once you find it, click on it and install it.

Generating the Executable File (Linux)

To generate the executable file for your Python script, follow these steps in your terminal:

1. Open the terminal.

2. Type in the following command:

pyinstaller --onefile --windowed --clean meter_converter.py 

Here's what each option does:

--onefile: This option creates a single executable file.

--windowed: It prevents the command line from being displayed when running the executable.

--clean: Use this option to overwrite any previously generated files if necessary.