Converting Fille Python to Exe - AntStack
IMG_1123.jpg Varun Gupta
7 min read Sep 15, 2022

How to Convert the Python Script 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 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. 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? 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.
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 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
Screenshot 2022-08-10 at 3.10.17 PM.png
  • 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:
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 python executable file. Screenshot 2022-08-22 at 2.56.11 PM.png

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.

  1. 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.

  2. 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.

  3. 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.

  4. 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:

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.

Application Modernization Icon

Innovate faster, and go farther with serverless-native application development. Explore limitless possibilities with AntStack's serverless solutions. Empowering your business to achieve your most audacious goals.

Talk to us

Author(s)

Tags

Your Digital Journey deserves a great story.

Build one with us.

Recommended Blogs

Cookies Icon

These cookies are used to collect information about how you interact with this website and allow us to remember you. We use this information to improve and customize your browsing experience, as well as for analytics.

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.

Talk to us