Make Installer Exe
Make Installer Exe

Make Installer Exe

Maret 05, 2023
exe file

Python is a versatile and widely-used programming language, but running Python scripts usually requires a Python interpreter installed on the machine. To distribute your Python applications without requiring users to install Python, you can convert your Python script into an executable file (.exe). This tutorial will guide you through the process of creating an executable file from a Python script using PyInstaller.

Step 1: Install PyInstaller

First, you need to install PyInstaller. You can install it using pip:

pip install pyinstaller

Step 2: Prepare Your Python Script

Ensure your Python script is ready. For this tutorial, we'll use a simple example script called hello.py:

# hello.py
print("Hello, World!")
input("Press Enter to exit...")

Step 3: Use PyInstaller to Create the Executable

Open a command prompt or terminal and navigate to the directory containing your Python script. Use the following command to create an executable:

pyinstaller --onefile hello.py

Explanation of the command:

  • pyinstaller: The command to run PyInstaller.
  • --onefile: This option packages everything into a single executable file.
  • hello.py: The name of your Python script.

Step 4: Locate the Executable File

After running the command, PyInstaller will create several new directories and files in your script's directory. The executable file will be located in the dist directory:

your_script_directory/
├── build/
├── dist/
│   └── hello.exe
├── hello.spec
└── hello.py

Step 5: Run the Executable

Navigate to the dist directory and run the executable to test it. You should see the output of your script in a console window.

Additional Customization

You can customize the PyInstaller command with various options:

  • Add Data Files: Include additional files such as configuration files or data files using the --add-data option. For example, to include a config.json file:
    pyinstaller --onefile --add-data "config.json;." hello.py
  • Icon: Add an icon to your executable using the --icon option:
    pyinstaller --onefile --icon=myicon.ico hello.py
  • Console Window: If your application is a GUI application and you don't want a console window to appear, use the --noconsole option:
    pyinstaller --onefile --noconsole hello.py

Example: Creating an Executable with Data Files

Let's say you have a Python script that reads from a config.json file and you want to package both into an executable.

Directory Structure:

your_script_directory/
├── config.json
└── read_config.py

Python Script (read_config.py):

import json

def main():
    with open('config.json', 'r') as f:
        config = json.load(f)
    print("Config content:", config)
    input("Press Enter to exit...")

if __name__ == "__main__":
    main()

Create the Executable:

pyinstaller --onefile --add-data "config.json;." read_config.py

Conclusion

By following these steps, you can easily create an executable file from a Python script using PyInstaller. This allows you to distribute your Python applications without requiring users to have Python installed. PyInstaller offers many options to customize the executable, making it a powerful tool for packaging Python applications.

For more detailed information, refer to the PyInstaller documentation.