Convert Your Python Code into a Windows Application (.exe file) | by Ujjwal Dalmia | Aug, 2020

0
8

A snappy tip for unrestricted sharing

Ujjwal Dalmia

You wrote an excellent Python software and offered it on your boss. He is inspired and needs to apply it to his device. He neither has Python put in on his device and nor he has ever labored on it. You are caught!!!

If the above sounds acquainted, then this educational will resolve your downside. Here, we can be told the method of changing a Python code to a Windows executable report. From now onward, each time you need to percentage your superb paintings with the broader neighborhood, you don’t have to fret about putting in the Python atmosphere on their techniques. Just create an executable and ship it to them. They will have the ability to use the appliance, similar to you do to your device.

1. A script to transform

For this educational, we have now written a small Python code that reads a ‘.csv’ report from the Windows folder location. This report has 2 columns, every containing a set of random numbers. The code creates a new column that accommodates the sum of numbers from the two enter columns. The changed report is stored on the identical folder location because the outdated one.

#### Importing the specified library
import pandas as pd
#### Reading csv report
df = pd.read_csv("C:UjjwalNew_File_V1.csv")
#### Adding 2 columns
df["Third_Column"] = df["Randome Numbers 1"] + df["Random Numbers 2"]
#### Exporting the information to identical location
df.to_csv("C:UjjwalNew_File_V2.csv",index = False)

Sample columns of the enter CSV report are as follows:

The output report after the code execution looks as if underneath:

2. Pyinstaller package deal

To convert the Python code into an executable report, we can be the use of Pyinstaller package deal. Use the usual ‘pip set up’ command to put in this package deal.

#### Install Command
pip set up pyinstaller

Let’s cross step by step to transform the Python report to a Windows executable:

  • Open the command instructed— The conversion of the Python script to Windows executable is finished the use of the command line. For this goal, we need to cross to the command instructed. Type “cmd” on your Windows seek field and open the command instructed
  • Change folder location — Use the next command and direct the command instructed to the positioning of your Python code:
#### Change Folder Location
cd folder_location
  • Conversion— Use the next command to transform the Python report into a Windows executable:
#### Command for conversion
pyinstaller --onefile filename

The above code will create a unmarried executable report with the similar capability as your python code. The executable report shall be to be had in a new folder, dist, which shall be to be had on the identical location as your Python script.

  • Execution — To execute the report, simply double click on the executable and it’ll produce the similar effects as your Python script.

There are some commonplace questions/demanding situations confronted by folks when operating with pyinstaller. This phase will solution maximum of them:

Additional recordsdata

Apart from the dist folder, which accommodates the executable report, different recordsdata and folders are getting created. You don’t want them. You can percentage your executable report with out those further recordsdata. Even when you’ll delete the extra recordsdata and folders, your executable won’t lose its capability.

Time to finishing touch

Both, introduction of the executable report and execution of the executable report is a time-consuming process. For a brief script like ours, the introduction of the executable took just about five minutes and the execution takes just about 30 seconds.

Size of the executable

Since we’re uploading applications in our Python script, to make the executable self-sufficient, the whole package deal will get integrated within the executable report. This will increase the scale of the executable report. For our case, it was once greater than 300 MB

Executable report failed with error

The maximum commonplace error which one can face when executing the executable report is “ModuleNotFoundError: No module named module title”. A pattern screenshot of this mistake and the real error instructed is as follows:

#### Error Prompt Message
ModuleNotFoundError: No module named 'pandas._libs.tslibs.nattypes'

If you come across an error like this (module title can also be other) then take the next steps:

  • Go to the Windows location the place the pyinstaller package deal is put in. Since I’m the use of Anaconda distribution, the next is the positioning on my device:
#### Package Location
C:UsersUjjwalAnaconda3Libsite-packages
  • In the pyinstaller package deal folder, seek for the folder named hooks. This folder has hook recordsdata for many of the often used Python applications. Search for the hook report, for the Python package deal, which has raised the mistake. In our case it was once Pandas. The pattern hook report and its contents are as follows:
  • The reason why in the back of the mistake is the malfunctioning of the command the place ‘hiddenimports’ is getting initialized. Comment this observation and upload a new one through which the ‘hiddenimports’ is initialized with the similar module title which has raised the mistake. For our case, it was once ‘pandas._libs.tslibs.nattype’. The code line to be added is as follows:
#### Code line for hook report
hiddenimports = 'pandas._libs.tslibs.nattype'
  • Once the hook report is changed, put it aside and re-create the brand new executable. Before recreating, please be sure that the outdated executable report and related folders are deleted. If the mistake persists, proceed so as to add the opposite lacking modules on your hook report. Please observe that a couple of modules will have to be added as a checklist construction.
#### Code line after including a couple of modules
hiddenimports = ['pandas._libs.tslibs.np_datetime','pandas._libs.tslibs.nattype','pandas._libs.skiplist']

The ultimate hook report which we used for our instance seemed as follows:

Once the entire modules are added, the mistake gets resolved.

In the above educational, we have now attempted addressing a small problem that almost all folks have confronted in the future in time in our service.

I am hoping this educational was once informative and also you realized one thing new.

Will attempt to convey extra fascinating subjects in long term tutorials. Till then:

HAPPY LEARNING!!!!

LEAVE A REPLY

Please enter your comment!
Please enter your name here