Delivering Python Based Applications For Windows Users

Apr 3, 2021 | Blog

About Sujata Khedkar

Python is a well known language for rapid development.  Also, it is one of the most versatile languages because of rich libraries available in it. These days most of the organizations use Python for implementing Data science projects as it has many connectors to data science related libraries. At Ellicium, we are in love with Python and everything it has to offer. Ellicians have also written some articles expressing their love for it.

Yet, developing Python application is one thing and deploying them on user machines is another.  Deploying Python application can become a tedious activity as it involves below-mentioned things:

  • Installing application compatible Python version
  • Installing related Python packages
  • Setting up environment variables on user machines
  • Updating the application with newer version whenever needed

All of this can be bit hectic. I got the first-hand experience of it while working on an analytics project for our medical KPO client. Their existing analytics was based on combinations of programming languages including Python. Our task was to empower it further. We had developed Python based applications for the same and we wanted few business users to test the beta version of it. While planning the deployment process, we realized that as a perquisite for deploying the application, we will have to install a lot of related packages for each of the programming languages.

Quite evidently, it turned into a nightmare!

After uncounted rounds of coffee, we found a better way to do it. This profound way is called ‘Creating Executables’. It triumphs over the hectic way of deploying a set of code on user’s machine.

There are multiple ways to create executables for Python code!

Ways to create executables for Python code

A) Using Python’s py2exe module

1) You can simply install py2exe by running pip on command prompt

pip install py2exe

2) Make sure the script that you have developed is syntactically and functionally correct.

3) Fetch a list of packages you have used in your Python script, which can be as follows:

import math
import Pyqt4

4) Create and open setup.py script to create exe from it and write following content in it:

from distutils.core import setup

import py2exe

options = {‘includes’:[‘math’]}            #Here you must add list of identified modules

setup(windows=[‘my_script.py’])        #Here mention your driver python script name

Here, instead of windows, you can also use console option so that you can track errors or warnings while running exe.

5) There is another case, if you want to add data file such as gif, images or any other files while running application then you must add them as:

Mydata_files = [(‘images’, [‘c:/path/to/image/image.png‘])]       #Here mention file name

setup(

windows=[‘my_script.py’],

data_files = Mydata_files)

6) Save and Close the setup.py file.

7) Now, to generate .exe file in the required directory use following command in command prompt:

python setup.py py2exe

8) If exe is created successfully then as aftereffect two folders will be formed. These are named as dist and these will be created within the same directory. You will also find exe in dist folder.

9) Now you are ready to deploy the application by simply placing these folders on the client machine without installing any programming language packages!

B) Using Python’s Pyinstaller module :

1) You can simply install pyinstaller using pip on cmd:

pip install pyinstaller

2) Use the command prompt to traverse to the path where your scripts are placed and execute the following command:

python pyinstaller.exe -–windowed –onefile your_main_file.py

3) Follow step 8 of py2exe method

You might be wondering which method to choose out of these two. To make it easy below is the comparison between these two methods:

                 Diagram: Comparison between py2exe and pyinstaller

Recommendation:

I would recommend using Pyinstaller as it is easy to create exe using pyinstaller than py2exe. Pyinstaller needs only single command to create exe whereas py2exe requires setup.py script to be created first and then command to create exe.

In case you want to know more about this, you can get in touch with me here: [email protected]

Want new articles before they get published?

Subscribe to our Blog.

[email-subscribers-form id="1"]