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 make a small project to see how it works.
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? To answer this question, we need to know what precisely virtual environments do.
- 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
.
- 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)
- 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:
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