Thursday 27 April 2023

How to create .exe in Python?

To create an executable file (.exe) from a Python script, you can use a library called PyInstaller. PyInstaller is a third-party module that can bundle Python scripts into standalone executables.

Here are the steps to create an executable from a Python script using PyInstaller:

Install PyInstaller: You can install PyInstaller using pip, the package manager for Python. Open a terminal/command prompt and enter the following command:

pip install pyinstaller

Create a Python script: Write the Python code that you want to convert to an executable file. Save the script as a .py file.

Generate the executable: Once you have your Python script ready, you can use PyInstaller to generate the executable file. Open a terminal/command prompt, navigate to the directory where your script is located, and enter the following command:

pyinstaller --onefile main.py

Replace "your_script_name.py" with the actual name of your Python script.

The --onefile option tells PyInstaller to create a single executable file, as opposed to a folder containing the executable and other files.

Locate the executable: PyInstaller will create a folder named dist in the same directory as your Python script. Inside the dist folder, you will find the executable file.

That's it! You have successfully created an executable file from your Python script using PyInstaller.