Home Audience Developers Create an Exe File for your Python Application in Windows

Create an Exe File for your Python Application in Windows

2
45086

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 www.python.org. You can get Pywin32 from http://sourceforge.net/projects/pywin32/files/pywin32/. Pyinstaller can be downloaded from https://pypi.python.org/pypi/PyInstaller.
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 pyinstaller.py

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:

Locate.py
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
continue
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 re.search(sys.argv[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 re.search(sys.argv[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 l4wisdom.com\n”

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

Place the locate.py 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 pyinstaller.py --onefile locate.py
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.

2 COMMENTS

  1. This shows how to convert a single python file into an executable one.
    But i have to convert my whole application that includes set of python files.
    Any help on that?

LEAVE A REPLY

Please enter your comment!
Please enter your name here