How to Convert Python .py file to .exe

Spread the love

In Python whenever we create a script, by default it get saved with .py extension which is standard for python. To execute this, we need python environment i.e. libraries and supporting files. But think of a situation when any python script is to be shared with someone who has machine without python installed on it. In such cases we need .exe files which are self contained with the required dependencies, thus do not need dedicated environment file for python. Let see how to convert python .py file to .exe.

To get started we need to first install a utility called as PyInstaller. PyInstaller bundles python application and all its dependencies in single file. Please note that PyInstaller supports Python 3.5 & newer version. Its available for Windows, MacOS & Linux. In this tutorial, we will be working on Windows OS. For more information on PyInstaller, please access the documentation here.

PyInstaller Installation

To install it, open Windows Power Shell, you can search it and open as well.

Window’s Search Utility

On the PowerShell window, type below command to install PyInstaller & press Enter.

pip install pyinstaller
Windows PowerShell

As I have already installed it previoulsy, I am getting message stating “Requirement already satisfied”.

Installation of PyInstaller

Conversion Process

Once installation is done, you are ready to go. To convert the .py file to .exe, we need to use below command. After command execution, .exe file will be available in dist folder. in same directory from where command was executed.

pyinstaller -w -F <path to .py python file to be converted>

In above command,

  • -w : To hide Terminal Window during the exe file execution.
  • -F : To bind all dependencies in single file. If we skip this option, all dependent files will be available in the dist folder, which is not what we want.
Conversion Process
Conversion Process

That’s it! The exe file will be available in dist folder.

Use of Custom Icon

By default the icon assigned to the converted exe file will be

If you want to change the default icon to the custom one, then you can do this with option “-i“. Only requirement is that the image you want to use should be in .ico format. There are multiple online convertors available which you can use to convert .jpg or .png files to .ico format. Once you have the custom image with ico format, you need to re-execute the previous command, but this time with additional “-i” option as below.

pyinstaller -w -F -i "<path to custom image file>" <.py file>

So easy right!


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

error: