How to convert .py to .exe?

If we want to run a python application on a windows platform without installing python, we have to convert the .py file to .exe. So that everyone can take advantage of that developed application. In this post, we will see how to convert the .py file to a .exe file using the python library name pyinstaller.

Step 1 :- Install the library pyinstaller using pip.

To install the library using pip, first, we have to go to the folder where your python is installed. In my case, the path of installation of python is C:\Users\Administrator.MIS-NB\AppData\Local\Programs\Python\Python37-32. In your case, it may be different.

Note: Here AppData folder may be hidden. In order to make it visible you have to change show hidden folder options in file explorer.

After finding your Python folder, get inside of Scripts folder. Click on address bar select whole address and type cmd over there after that hit enter.

You will get above command prompt where you can type following command.

pip install pyinstaller

After successfully installing pyinstaller you can convert .py file into .exe file.

Step 2: Go to the folder where your .py file is present.

Step 3: Run the following commands as per your requirements

Keep pressing the shift button and right-click in that folder which will give you options. Select open command window options from there, which will give you a command prompt

Command 1

pyinstaller -w

Above command will be usefull when you don’t want console.

After hitting enter it will start converting the python file into an executable file. At last, it will show a success message, which means your .py file has been successfully converted into a .exe file.

After executing the above code your .py file containing the folder will look like this. Over here build folder and login. the spec file is of no use you can delete them if you want it’ll not affect your .exe file. dist folder contains your actual .exe file.

Dist folder contains login folder, this is where our login.exe file is situated and it looks like below. Double click on it to get output.

This is my login.exe output

Command 2

pyinstaller -F

This will give you a command prompt. This command is useful when you are printing anything on the python shell. The executable file created by this command lies inside the dist folder and if you double click that .exe file, it gives output as follows.

This is the output of second command.

Command 3

pyinstaller -i iconfilename.ico

The above command is used to set the icon for the developed application. But one important thing to note here is python accepts images having only the .ico extension and makes sure that this image file is stored where your .py file is located or else you have to specify the full path for the image. Sometimes it takes time to set the icon on the .exe file.

Output of above code return default command prompt, if you don’t want command prompt in the output use following command.

Command 4

pyinstaller -w -i iconfilename.ico

Leave a Comment

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