The Complete Magazine on Open Source

Create an Exe File for your Python Application in Windows

Python visual

You can build your own search tool for Windows, using Python and allied technologies, by going through this tutorial. This tool will search for files which are located on a pen drive too. You can also hone your Python and programming skills by trying out this tutorial.

This article is for those with basic Python programming skills, looking to build simple Windows executable programs using Pywin32 and Pyinstaller. It doesn’t provide solutions for building complex software programs, though.
There are a few prerequisites to creating your exe file, which are mentioned below:

  • Windows OS
  • Python 2.7.x
  • Pywin32
  • Pyinstaller

Python 2.7.x can be downloaded from You can get Pywin32 from Pyinstaller can be downloaded from
In this article, we will try to cover the basics of creating a Windows executable with the help of Pywin32 and Pyinstaller. While installing Python, please select the option to add it into the class path variable of the system. After installing Python, open the command prompt window and type the following command:

pip install pyinstaller
Figure 1

Figure 1: Output of

Pywin32 is an executable file and can be installed easily.
After completing the installations, let’s write a simple Python program file which will help to locate any file in the whole system, including external devices such as a pen drive. This Python file is built as an executable and placed in the Windows folder. The command locate is run with a combination of search strings such as the name of the file.
Given below is the code for the Locate program in Python:
import os
import re
import sys
from datetime import datetime

response = os.popen(“wmic logicaldisk get caption”)
list1 = []
for line in response.readlines():
line = line.strip(“\n”)     # this line strip the \n
line = line.strip(“\r”)  # this line strip the \r means carriage return
line = line.strip(“ “)  # use to strip blank space
if (line == “Caption” or line == “”):  # if caption and “” appears this line ignore
list1.append(line)  # list1 now contain all drives of windows
a = “\\”
def search1(list1):
tf = 0
td = 0
for each in list1:      # take each drive one by one
print “In “, each
for root, dir, files in os.walk(each, topdown = True):   # this line return the root dir (means start from drive),  sub driectory and files in the root dir
for file in files:    # start search from files
if[1].lower(), file.lower()):  # this line match the substring given in command line argument with files one by one
print root+a+file # if match found then make a path to print
tf = tf+1 # to count the files
for d in dir:
if[1].lower(), d.lower()):  # this line for directories
print root+a+d  #print the path
td = td+1 # print the number of dirs matched
print “Total File(s) are “, tf
print “Total Directores are “, td
print “\nThank you for using\n”

search1(list1)   # call the function.
total =t2-t1  # to calculate the total time taken in process
print “Searchingcompleted in “ , total

Place the file in the Pyinstaller folder and open the command prompt. Go to the Pyinstaller folder and then execute the following command:

C:\PyInstall-2.1>Python --onefile
Figure 2

Figure 2: Locate command using the command prompt

After running the command, you will get an output as shown in Figure 1.
Place the locate.exe file in the Windows folder of the system. This will make the system automatically pick the file through the Windows path variable. Now, go to the run command and provide the command locate, along with the file name you are looking for. If you don’t remember the file name, then this command also takes the search query, as shown in Figure 2.

  • Uday Prakash

    Ensure that indentation is maintained; else the source code is of no use.