This article aims at how to convert a python script into a windows executable(.exe) file.
You must have seen all windows application/software create a .exe file which is an executable file after installation. This .exe contains all the information about the packages, resources, libraries which will be used to run the program on the system.
Now, Coming to the python, all python scripts are saved as .py extension which can only be executed using the python interpreter.
So, if you want to share or distribute your python software you must have to install python interpreter on the system to run the program. This method is not user-friendly and does not guarantee a successful execution of your script due to library version problem etc.
So to tackle this problem you can convert your .py file into a .exe file which will be a stand-alone file and can be executed on any windows system.
Steps to convert a .py file into .exe file
- Write your python script as usual and save it with .py extension.
- Check Your script is giving the desired output.
- Create a new virtual environment in anaconda.(optional check Step-by-step anaconda installation and how to create virtual environment)
- Activate the virtual environment.
- Install pyinstaller a python package.
- change the current directory in terminal / cmd prompt to the directory where you saved your .py file.
- Convert the .py file into .exe file using the command
pyinstaller --onefile cui_exe.py(replace cui_.py with your file name)
- Open the dist folder created in the working directory.
- Run the cui_exe.exe file.
- Congratulations you have just created your first distributable Windows software.
- To make a GUI software use the command
pyinstaller --onefile --windowed gui_exe.py
this will by default run the GUI which you created using TKinter or any other GUI framework
Further, You can also add an icon to your exe file by using the command
- Convert image into .ico
- Save image in the same directory where the .py file is located.
pyinstalled --onefile --windowed --icon=icon.ico gui_exe.py