Make a python file stand-alone (.exe) file for windows.

Make a python file stand-alone (.exe) file for windows.

This article aims at how to convert a python script into a windows executable(.exe) file.

Skip to the Tutorial

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

  1. Write your python script as usual and save it with .py extension.
  2. Check Your script is giving the desired output.
  3. Create a new virtual environment in anaconda.(optional check Step-by-step anaconda installation and how to create virtual environment)
  4. Activate the virtual environment.
  5. Install pyinstaller a python package.
    Conda installs pyinstaller
    pyinstaller installation
  6. change the current directory in terminal / cmd prompt to the directory where you saved your .py file.
  7. Convert the .py file into .exe file using the command
    pyinstaller --onefile (replace with your file name)
    pyinstaller py to exe
  1. Open the dist folder created in the working directory.
    dist folder pyinstaller
  2. Run the cui_exe.exe file.
    exe file run
  3. Congratulations you have just created your first distributable Windows software.
  4. To make a GUI software use the command
    pyinstaller --onefile --windowed
    GUI exe
    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

  1. Convert image into .ico
  2. Save image in the same directory where the .py file is located.
  3. pyinstalled --onefile --windowed --icon=icon.ico
    icon to exe
Loading comments...