Which service provides serverless computing in Azure?
Nowadays, we can automate our daily minor tasks just by writing a script and running it on a terminal. And as we speak of a scripting language, Python is the fastest and easiest scripting language. So, on a daily basis, you just don’t want to open a terminal and run the same command rather, you’d prefer an executable file that you can run on any system just by double-clicking on it.
It is possible to do so with our beloved Python language, and we don’t need to install Python to run it on our other system too. Isn’t it awesome? Let’s do a small project to see how you can convert Python script to exe file.
Convert Python to exe in 5 Steps
Before we get started, here are the elements we will use.
Pre-requisite:
- Python3.8
- Pip
- Computer System, for this activity we are going to use macOS.
* You can use the Windows system as well, the steps will be different as mentioned below.
Instructions: Now, let us jump right into the steps involved in taking out the python script exe an executable file. Note: If you have already installed Python3 you can skip step1.
Step 1 - Installing Python3 into the system:
As we are working with python we need to install python3 into our system. You can use terminal commands to install or you can visit the mentioned link to install via the setup process. In this project, we are going to use python3.8.13 link. Using terminal:
macOS:
- Install using brew:
brew install python@3.8.13
- Check the version after installation:
python –version
- Install using brew:
Windows:
- On your system, download the python.exe file from the official website.
- Open CMD and change your directory to the path where you have python.exe
- Paste the code in your Command prompt make sure to change the name with your file version In the below code(e.g python-3.8.13.exe)
python-3.8.13.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
Step 2 - Creating a virtual environment:
Why do we need to create a virtual environment in the first place? We need to know what virtual environments do to answer this question.
- Suppose we have a folder that has all the dependencies and tools required to make that python file work i.e. located inside your folder but we don’t need that dependencies and tools to interfere with our other projects.
- So, we use a virtual environment that will isolate all the dependencies and tools that are required by our projects from our other projects.
Now, let’s create our virtual environment:
Open Terminal. Head towards the directory in which you want to create this project. In our case it is python-exe.
For macOS:
- Execute this command in terminal:
python3 -m venv pyenv
.
- Execute this command in terminal:
- For Windows:
- Execute this command in cmd:
virtualenv pyenv
- Execute this command in cmd:
Now we need to activate the virtual environment, that we just created:
source pyenv/bin/activate
(macOS)
source pyenv/Scripts/activate
(Windows)
Step 3 - Installing python modules:
Now we need to install all the necessary python modules that we are going to use in our python activity. - Pyinstaller - Pygame
- PyInstaller will bundle a python application and all its dependencies into a single executable file then Without installing a python interpreter or any modules, the user can run the packed program.
- Pygame is a free and open-source library for utilizing python to create apps like video games. As we are developing a small game called snakes.
* Before you install modules update your pip installer.
pip install pygame pyinstaller
Step 4 - Creating game file:
Now, we just need to create our game file or you can say game.py. The code inside the game.py file is in the Github repo link.
Step 5 - Packing the game.py file into an python executable file:
Now we have the game code file, we just need to pack the file with all the other dependency modules used in the game.py.
- Execute the command to get the python executable file for our python code:
pyinstaller –onefile game.py
- Here,
–onefile
flag will give only one python executable file in the dist folder. You can also try running the above command without using–onefile
flag to see the difference. game.py is the file name that will be packed with other dependency modules. After running the command you will find the logs ending with this:
Why Convert Python Script to exe Files?
Now that we have learned how to convert a Python to executable file, let’s also look at some reasons as to why you should do it.
Ease of use Executable files are more user-friendly, and users can run them by simply double-clicking. These files don’t need you to open a command prompt/terminal and type out commands to run the script.
Accessibility Converting Python to exe is particularly useful when you share an application with a user who may not be familiar with Python. A typical Python program consists of several dependencies that the end-user would have to install so they can run the application.
However, an exe file is a standalone executable, which means you can distribute it to other users, and the end user can run it on a target machine without installing Python.Portability An executable file can compress your Python code and the required dependencies. This makes it portable while reducing compatibility issues with different operating systems.
Hiding Implementation Details Converting Python to exe files when distributing a closed-source application can make it difficult for users to access and modify the code.
Conclusion
Converting a Python script to an executable file can simplify the distribution and execution of your programs. In this article, I have outlined the steps for macOS and Windows. Let us know how useful these steps were.
References:
- Python Command Line: https://docs.python.org/3.8/using/cmdline.html
- Python pyinstaller: https://pyinstaller.org/en/stable
- Python pygame: https://www.pygame.org/docs/
- Python virtual env: https://docs.python.org/3.8/library/venv.html
FAQs
1. What are the limitations of converting Python scripts to exe files?
Some common challenges include:
- Large file size due to bundled dependencies
- Execution speed might be slower than a native application
2. Are Pyhton executables cross-platform?
No. Executables built on one platform won’t run on another platform. For cross-platform compatibility, you can use tools like cx_Freeze
or create platform-specific builds.