Sep 15, 2022 5 min read How to Convert the Python File Into an Executable File

How to Convert the Python File Into an Executable File

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 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. Screenshot 2022-08-24 at 2.28.57 PM.png Using terminal:

  • macOS:

    • Install using brew: brew install python@3.8.13
    • Check the version after installation: python –version
  • 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.

Screenshot 2022-08-10 at 3.08.47 PM.png To explain the above command: * python3 - is what we are using to create this virtual environment. * -m - Searches the system path for the named module and executes its contents as the main module. * venv - virtual environment module * pyenv - the name of the virtual environment

  • For Windows:
    • Execute this command in cmd: virtualenv pyenv

Now we need to activate the virtual environment, that we just created: source pyenv/bin/activate (macOS) source pyenv/Scripts/activate (Windows)

Screenshot 2022-08-10 at 3.09.03 PM.png

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. Screenshot 2022-08-10 at 3.09.15 PM.png pip install pygame pyinstaller Screenshot 2022-08-10 at 3.09.42 PM.png Screenshot 2022-08-10 at 3.09.25 PM.png

Step 4 - Creating game file:

Now, we just need to create our game file or you can say game.py. Screenshot 2022-08-10 at 3.09.55 PM.png The code inside the game.py file is in the Github repo link.

Step 5 - Packing the game.py file into an 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 executable file for our python code: pyinstaller –onefile game.py

Screenshot 2022-08-10 at 3.10.17 PM.png

  • Here, –onefile flag will give only one 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:

Screenshot 2022-08-10 at 3.10.33 PM.png

  • Now, we can head to the directory where we have placed our project:
  • We will find two folders and one .spec file has been created.

Screenshot 2022-08-10 at 3.11.28 PM.png

  • In the build folder, we will find a game folder that has all the files related to packing the executable file.
  • And, In the dist folder, we will find our game executable file. Screenshot 2022-08-24 at 8.42.56 PM.png
  • There is one more file game.spec which contains the specification to take out the executable file.
  • Now, let’s run the executable file.

Screenshot 2022-08-22 at 2.56.11 PM.png

References:

Author(s)

Topics

Share this blog

5 min read

Sep 15, 2022

This website stores cookies on your computer.

These cookies are used to collect information about how you interact with this website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors on this website.

If you decline, your information won’t be tracked when you visit this website. A single cookie will be used in your browser to remember your preference not to be tracked.