Convert Your Python Code into a Windows Application (.exe file) | by Ujjwal Dalmia | Towards Data Science

You wrote an amazing Python application and presented it to your boss. He is impressed and wants to use it on his system. He neither has Python installed on his system and nor he has ever worked on it. You are stuck!!!
If the above sounds familiar, then this tutorial will solve your problem. Here, we will learn the process of converting a Python code to a Windows executable file. From now onward, every time you want to share your excellent work with the wider community, you don’t have to worry about setting up the Python environment on their systems. Just create an executable and send it to them. They will be able to use the application, just like you do on your system.
For this tutorial, we have written a small Python code that reads a ‘.csv’ file from the Windows folder location. This file has 2 columns, each containing a set of random numbers. The code creates a new column that contains the sum of numbers from the 2 input columns. The modified file is saved at the same folder location as the old one.
#### Importing the required libraryimport pandas as pd#### Reading csv filedf = pd.read_csv("C:\\Ujjwal\\New_File_V1.csv")#### Adding 2 columnsdf["Third_Column"] = df["Randome Numbers 1"] + df["Random Numbers 2"]#### Exporting the data to same locationdf.to_csv("C:\\Ujjwal\\New_File_V2.csv",index = False)
Sample columns of the input CSV file are as follows:
The output file after the code execution looks like below:
To convert the Python code into an executable file, we will be using Pyinstaller package. Use the standard ‘pip install’ command to install this package.
#### Install Commandpip install pyinstaller
Let’s go step by step to convert the Python file to a Windows executable:
Open the command prompt— The conversion of the Python script to Windows executable is done using the command line. For this purpose, we have to go to the command prompt. Type “cmd” in your Windows search box and open the command prompt
Change folder location — Use the following command and direct the command prompt to the location of your Python code:
#### Change Folder Locationcd folder_location
Conversion— Use the following command to convert the Python file into a Windows executable:
#### Command for conversionpyinstaller --onefile filename
The above code will create a single executable file with the same functionality as your python code. The executable file will be available in a new folder, dist, which will be available at the same location as your Python script.
Execution — To execute the file, just double click the executable and it will produce the same results as your Python script.
There are some common questions/challenges faced by people when working with pyinstaller. This section will answer most of them:
Apart from the dist folder, which contains the executable file, other files and folders are getting created. You don’t need them. You can share your executable file without these additional files. Even when you will delete the additional files and folders, your executable will not lose its functionality.
Both, creation of the executable file and execution of the executable file is a time-consuming task. For a short script like ours, the creation of the executable took close to 5 mins and the execution takes close to 30 seconds.
Since we are importing packages in our Python script, to make the executable self-sufficient, the complete package gets incorporated in the executable file. This increases the size of the executable file. For our case, it was more than 300 MB
The most common error which one can face when executing the executable file is “ModuleNotFoundError: No module named module name”. A sample screenshot of this error and the actual error prompt is as follows:
#### Error Prompt MessageModuleNotFoundError: No module named 'pandas._libs.tslibs.nattypes' 
If you encounter an error like this (module name can be different) then take the following steps:
Go to the Windows location where the pyinstaller package is installed. Since I am using Anaconda distribution, the following is the location on my system:
#### Package LocationC:\Users\Ujjwal\Anaconda3\Lib\site-packages
In the pyinstaller package folder, search for the folder named hooks. This folder has hook files for most of the commonly used Python packages. Search for the hook file, for the Python package, which has raised the error. In our case it was Pandas. The sample hook file and its contents are as follows:
The reason behind the error is the malfunctioning of the command where ‘hiddenimports’ is getting initialized. Comment this statement and add a new one in which the ‘hiddenimports’ is initialized with the same module name which has raised the error. For our case, it was ‘pandas._libs.tslibs.nattype’. The code line to be added is as follows:
#### Code line for hook filehiddenimports = 'pandas._libs.tslibs.nattype'
Once the hook file is modified, save it and re-create the new executable. Before recreating, please ensure that the old executable file and associated folders are deleted. If the error persists, continue to add the other missing modules in your hook file. Please note that multiple modules should be added as a list structure.
#### Code line after adding multiple moduleshiddenimports = ['pandas._libs.tslibs.np_datetime','pandas._libs.tslibs.nattype','pandas._libs.skiplist']
The final hook file which we used for our example looked as follows:
Once all the modules are added, the error will get resolved.
In the above tutorial, we have tried addressing a small challenge that most of us have faced at some point in time in our carrier.
I hope this tutorial was informative and you learned something new.
Will try and bring more interesting topics in future tutorials. Till then: