How To Convert Python Script Into Executable .exe File

Introduction

 
In this blog, you will see how to convert a Python script into an executable .exe file, using pyinstaller. I have created a Python script which will rename a file with the following details and screenshots.
 
Before making a script to an exe, we need to create a script which will rename the file.
 

Source Code to rename a file

 
import os, sys
src=r'C:\Users\Pictures\unnamed.jpg'
dest=r'C:\Users\Pictures\Nature.jpg'
os.rename(src,dest)
 
 

Source Code Description

 
We import OS and SYS modules. In Python3, the rename() method is used to rename a file or directory. This method is a part of the OS module.
 
Syntax of os.rename()
 
os.rename(src, dst)
 
src is the source address of the file to be renamed and dst is the destination address with the new name.
 

Output

 
Without user intervention, the file was changed from unnamed.jpg to Nature.jpg.
 
 
Once the script is ready, follow the below steps to convert the .py file into .exe file.
 

Steps

  1. Open Command Prompt and run pip install PyInstaller, to install Pyinstaller. 
  2. On successful installation of pyinstaller, using the Command prompt, navigate to the location where your source code is placed. (I recommend to put your .py script into a separate folder)
  3. Cmd to convert .py to exe -- > pyinstaller renameFile.py, 
     
     
  4. On successful completion of the building process, you should see the below file structure.
     
     
  5. Navigate to dist folder for Python 3 installed user. Under dist folder, there will be a folder which is the same as your .py file name, in which you search for your file name with Application type.
     
     
  6. Finally, your .py file has been converted into .exe file.
Note
  • pyinstaller renameFile.py -- will generate .exe but when we run, we get console popping up.
  • pyinstaller –w renameFile.py -- will hide Console popping you when we run.
  • pyinstaller –F renameFile.py -- this will create only the .exe file in your dist folder.
  • pyinstaller –i [icon file] renameFile.py -- this will create an icon to your .exe [icon file has to be .ico extension else it fails].

Conclusion

 
Finally, combine any of these parameters to generate your desired .exe for your Python script.