How to Compile Python to Exe ? 3 Methods

Compile Python to Exe Featured Image

Are you searching for how to compile Python to exe? Here are some easy tricks to achieve with the help of these modules- Pyinstaller, auto-py-to-exe, Py2exe.

Why there is a need to convert Python to Exe?

There are many advantages when you convert Python to Exe. Some of the main features are below.

Distribution Simplicity

It’s very easy to distribute exe files among the users. The end user does not need to install Python and libraries to run the Python executable file.

Dependency Issues

The user doesn’t face any dependency issues when running the Python executable file. This clears the headache faced by the coders while running the Python code.

System Integration

The executable file is easily integrated with the system like system tasks. You can see this executable file in your windows task manager or startup process.

Sample Python Script creation –

Before proceeding to this article, We need to create a sample Python script that will be used for the demonstration.  Here I am creating a sample python script that will output the sum of two numbers provided by the user –

 

input1 = input('Enter your first number: ')
input2 = input('Enter your second number: ')

#sum
sum = float(input1 ) + float(input2 )

# output
print('The sum of {0} and {1} is {2}'.format(input1 , input2 , sum))

The above sample is saved by the name of sum.py. You may download it directly from here.

1. Compile Python to Exe Using Pyinstaller –

 

This section will explain how to convert py to exe using pyinstaller?  All you need is to follow the below steps for compiling python to exe in Linux as well as Windows Operating System.

 

Step 1:  First, Install Pyinstaller using pip or any other package manager.

# For Python 2.xx version
pip install pyinstaller

# For Python 3.xx version
pip3 install pyinstaller

I have python 3.xx version, that’s why I am using the pip3 command.

Installing Pyinstaller using pip3 command
Installing Pyinstaller using pip3 command

 

Step 2:  Go to the path where the python script is stored. You may directly open the command prompt on that location using shift + Right Click, or change the path using cd command in prompt.

 

Python Script Directory
Python Script Directory

 

Step 3:  Most importantly, once you are on the same path where you have the sample python script (for example sum.py), Use the below command.

# sample python file is sum.py
pyinstaller --onefile sum.py

 

Python Script Dist Directory
Python Script Dist Directory

 

You will get the python exe file under the dist folder on the same location. I hope this quick pyinstaller tutorial on python will help you to get the exe conversion.

Python Script Exe File
Python Script Exe File

2. Compile a Python Program to Exe using auto-py-to-exe (GUI Tool) –

 

Using this GUI (Graphical User Interface) Tool, You can easily build python to exe in a few steps-

 

Step 1:  Firstly, Install the auto-py-to-exe using pip package manager –

 

# For Python 2.xx version
pip install auto-py-to-exe

# For Python 3.xx version
pip3 install auto-py-to-exe

 

Installing auto-py-to-exe using pip3 command
Installing auto-py-to-exe using pip3 command

 

Step 2:  Lunch this auto-py-to-exe, By entering auto-py-to-exe into cmd.

Command to run auto py to exe GUI
Command to run auto py to exe GUI

 

Step 3:  Once the application is launched, You need to select the python script path into Script Location field.

 

Selection of Python Script to Convert to exe
Selection of Python Script to Convert to exe

Step 4:  Now, You need to decide that you need the exe file as One Directory or as one file. You need to choose accordingly. Here if you so many python files dependencies for the application, You need to choose the main python file.

If you choose “One Directory” as an option you will get exe in the output folder (path is user-configurable) with dependencies file. If you opt “one file option”, your all code dependencies will be in one exe file.

 

Step 5: Simply press the button Convert .py to .exe and get the generated exe file into the output folder.

 

Exe File for the Python Script
Exe File for the Python Script

3. Convert Python File to exe using Py2exe-

Let’s see how can you use Py2exe to compile a python script to exe file.

Step 1:  Firstly install Py2exe. Using pip –

# For Python 2.xx version
pip install Py2exe

# For Python 3.xx version
pip3 install Py2exe

 

Installing Py2exe using pip3 command
Installing Py2exe using pip3 command

 

Step 2:  You need to create a new file setup.py at the same directory where you have the script. This file contains this below parameters-

 

from distutils.core import setup
import py2exe
setup(console=['sum.py'])

 

Here above sum.py is a dummy python script for demonstration for the py2exe functionality. Here you may put your own library.

 

Step3:   Now the last step is to launch the command prompt and type the below command to convert the python script to exe. Make sure you must be running the command from the same directory where the setup.py file is available.

 

python setup.py py2exe
Running the setup python script with Py2exe
Running the setup python script with Py2exe

This will create the executable file into dist folder in the same directory where we have the sample python script (sum.py)

Conclusion –

I hope the above solutions on compilation must be enough for the problem statement. Well, most if the things in this article is self explanatory . But if you need some other help related to the same subject, Please write back to us.

Thanks 

Data Science Learner Team 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner