Skip to content

Create .EXE

Benedikt Schächner edited this page Jul 5, 2023 · 2 revisions

In this entry, I will show you how to convert Python code to a standalone executable file (.exe) using PyInstaller. Additionally, we will also include an image in the build so that it can be used by the created application.

⬇️ Install PyInstaller

Make sure PyInstaller is installed on your system. If you haven't installed it yet, you can do so using the following command:

pip install pyinstaller

📁 Prepare the Files

Ensure that your Python script (e.g., text-editor.py) and the image you want to include (e.g., bold.png) are in the same directory.

📦 Convert the Code and Include the Image

Open your command prompt or terminal as Admin and navigate to the directory where your Python script is located. Execute the following command to convert the Python code to an executable file and include the image:

pyinstaller --noconfirm --onefile --windowed --add-data "bold.png;." text-editor.py

--noconfirm is used to bypass any confirmation prompts.
--onefile specifies that the output should be a single executable file.
--windowed creates a windowed application without a console window.
--add-data "bold.png;." includes the "bold.png" image in the build.

Make sure the image is in the same directory as your Python script. The dot (.) at the end of the path indicates that the image should be available in the program's working directory. text-editor.py is the name of your Python script. Adjust the name accordingly. After the command is successfully executed, a new directory named "dist" will be created. Inside that directory, you will find your executable file, which will have the same name as your Python script (e.g., text-editor.exe).

Note: Check the output of the command to ensure that no errors or warnings occurred. If necessary, additional dependencies or resources may need to be specified.

That's it! You have successfully converted your Python code to an executable file using PyInstaller and included an image in the build. You can use the generated .exe file from the "dist" directory. The other files and folders are not needed.

🥈 Alternative

You can also use python -m auto_py_to_exe and the visual installer.

Clone this wiki locally