10

Convert Your Python Code into a Windows Application (.exe file)

 3 years ago
source link: https://towardsdatascience.com/convert-your-python-code-into-a-windows-application-exe-file-28aa5daf2564
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Convert Your Python Code into a Windows Application (.exe file)

A quick tip for unrestricted sharing

Image for post
Image for post
Photo by Tadas Sar on Unsplash

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.

What do you need?

1. A script to convert

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 library
import pandas as pd#### Reading csv file
df = pd.read_csv("C:\\Ujjwal\\New_File_V1.csv")#### Adding 2 columns
df["Third_Column"] = df["Randome Numbers 1"] + df["Random Numbers 2"]#### Exporting the data to same location
df.to_csv("C:\\Ujjwal\\New_File_V2.csv",index = False)

Sample columns of the input CSV file are as follows:

Image for post
Image for post
Input CSV File (Image by Author)

The output file after the code execution looks like below:

Image for post
Image for post
Output CSV File (Image by Author)

2. Pyinstaller package

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 Command
pip install pyinstaller

The Actual Task

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 Location
cd folder_location
  • Conversion— Use the following command to convert the Python file into a Windows executable:
#### Command for conversion
pyinstaller --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.

FAQ

There are some common questions/challenges faced by people when working with pyinstaller. This section will answer most of them:

Additional files

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.

Time to completion

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.

Size of the executable

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

Executable file failed with error

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 Message
ModuleNotFoundError: 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 Location
C:\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:
Image for post
Image for post
Original Hook File (Image by Author)
  • 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 file
hiddenimports = '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 modules
hiddenimports = ['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:

Image for post
Image for post
Final Hook File (Image by Author)

Once all the modules are added, the error will get resolved.

Closing note

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:

HAPPY LEARNING!!!!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK