Thursday, November 7, 2024

Programming Scripting Automation: PyInstaller, Convert Python Into Standalone Executable Programs

PyInstaller illustration

    Hello there! Good day, and welcome to my blog. Today, I’d like to share some valuable insights on how to convert a program, script, or application into a standalone executable using PyInstaller. This process makes it much easier to distribute your program and allows others to run it without needing Python installed.

Let's dive in, I'll show you how it’s done


What Is PyInstaller?

PyInstaller is a popular tool used to convert Python scripts into standalone executable programs. This makes it possible for people to run Python programs on their computers without needing to install Python or any other dependencies.

PyInstaller version 6.11.0

If you’ve written a Python application that you want to share with others (who might not have Python installed), PyInstaller can help package your script and all its dependencies into a single executable file.

Also PyInstaller supports creating executables for Windows, macOS, and Linux, but you need to build the executable on the same platform you plan to distribute it for (for instance, you need to be on Windows to make a Windows .exe file).


Installation Of PyInstaller

To install PyInstaller, run the following command in your terminal:

pip install pyinstaller

To create an executable from your Python script, navigate to the directory containing your script, then run:

pyinstaller your_script.py

This will create several directories and files, including:

- dist/: Contains the packaged executable.
- build/: Temporary files during the build process.
- your_script.spec: A configuration file for PyInstaller to customize builds.

The output executable will be inside the "dist/your_script/" directory.

Two ways to use PyInstaller are:
  1. Using the terminal in Linux or Command Prompt (CMD) in Windows.
  2. Using auto-py-to-exe, the graphical user interface (GUI) for PyInstaller.

 

Using Terminal or CMD

For example, I want to convert my program YTDownloader into an executable:

  1. The name of the executable should be 'YTDownloader'.
  2. The executable should be created as a single file 'onefile'.
  3. The program will run in console mode 'console'.
  4. Use the 'clean' option to clear PyInstaller's cache and remove temporary files.
  5. The program is located at: "/home/myportfolioreview13/Documents/Python Demo/DEMO - YTDownloader V.4.4.9/YTVideoDownloader.py"
  6. '--noconfirm' option is used to suppress the confirmation prompt that appears when PyInstaller overwrites existing files during the build process.

This is the command:

pyinstaller --name "YTDownloader" --noconfirm --onefile --console --clean "/home/myportfolioreview13/Documents/Python Demo/DEMO - YTDownloader V.4.4.9/YTVideoDownloader.py"

PyInstaller in terminal

This is the resulted file in dist folder:

File result in dist folder


Using auto-py-to-exe (GUI) for PyInstaller

Open the terminal where the program is located, then type the following command in the terminal or Command Prompt (CMD): auto-py-to-exe.

Open the auto-py-to-exe GUI

Afterward, a web browser will open, displaying the GUI that allows you to configure the settings for converting the program.

The PyInstaller GUI

Set the configuration as needed and click the 'Convert' button.

Converting the program

The resulted file will be at 'output' folder.

Output from auto-py-to-exe

For a more detailed explanation of the process, watch a full video tutorial.

Video demo PyInstaller


End

Thank you for visiting and reading my blog. I look forward to seeing you again in the next article.