Do you want to know how to convert your Python script or file (.py) to an executable file (.exe)? This guide shows you 3 fool-proof methods to try!
A .py file extension is used when the file of a program or script is written in Python. So, these files ending with the .py extension can be written and modified with almost any text editor. However, if you want to run the file, you would need a Python interpreter.
On the other hand, a .exe file extension is used by Windows executable program files. EXE stands for “executable” and it’s very commonly used by programs on Windows which is why it may sound familiar to you. This is because EXE files contain code that can be easily understood and directly executed by a computer.
That being said, there are many reasons why someone may want to convert a .py file to .exe. Maybe you just created an app using Python and want your friend to test it. Besides that, maybe you need someone to run a script you just wrote to automate a task for them. So, double-clicking on an executable file is much easier than for example, downloading and installing Python and running the file from the command line.
Fortunately for us, there are tools that we can use to do this in a few simple steps.
In this guide, we’ll go over how to convert a .py file to .exe on Windows, Linux, and macOS.
Table of Contents
Using pyinstaller to convert .py file to .exe (Windows, Linux, and macOS)
PyInstaller is a Python package that compiles a Python application and its dependencies into one package. So, this packaged app can then be executed by the user without the need to install the Python interpreter or any other modules.
Before we begin the conversion of a file from .py to .exe, we will need to install a package called PyInstaller. In order to install a package using the native command line, simply enter:
pip install pyinstaller
For this example, I will be using a simple program called test.py that prints out a string. Once PyInstaller has been installed, proceed to:
- First, navigate to your project’s folder using the command line.
- Then, enter pyinstaller test.py where test.py is your filename
Once you see this at the end of the command line, you’re good to go!
- Next, navigate to your project’s folder. There will be a few new files and folders created in your project’s folder, one of them being called dist. Open the dist folder and there will be another folder called test.
Inside this test folder, there will be an exe file named after your original python file. In this case, it is named test.exe.
When double-clicked, a Command Prompt window will open and the code will be executed.
This same method can be used to convert files from .py to .exe on Linux and macOS as well.
Using cx_freeze to convert .py file to .exe
Another way to convert .py files to .exe is by using cx_freeze. Cx_freeze is used to create executable files from python scripts and it is cross-platform.
Before starting, let’s say we want to convert a program called test.py. To use cx_freeze, we have to:
- First, create a file called setup.py in the same folder where test.py is located.
- Then, edit the setup.py file to contain the following:
from cx_Freeze import setup, Executable setup(name = "test", version = 1, executables = [Executable("test.py")])
- After that, open the Command Prompt and navigate to the folder where setup.py and test.py are located.
- Next, enter the following command:
python setup.py build
Once completed, the test.exe file will be found in a folder that is located inside the same folder as setup.py. For example, my setup.py is inside a folder called test. Hence, my test.exe can be found in test\build\exe.win-amd64-3.8.
Using Auto PY to EXE application to convert one or more .py files to .exe (Windows)
Auto PY to EXE is a GUI application that makes it extremely easy and straightforward to convert files. In order to use Auto PY to EXE, we have to first install it through the command line:
- First, open the cmd (Command Prompt) and enter the following command:
pip install auto-py-to-exe
- Once installed, open it by entering the following command:
Then, a GUI should appear on your screen.
The next step is to browse and select the .py file that you wish to convert to .exe.
Once the file has been selected, you need to choose either the One Directory or One File option. One Directory essentially means that all the files and dependencies related to the project file will be placed in a single directory. One File means that the application will create a single .exe file consisting of all project dependencies excluding media files.
For this example, I will be proceeding with the following configuration:
Once the conversion has been completed, you will see a similar message in the output box:
So, proceed to the folder mentioned to find the converted .exe file:
You can read more about using Auto PY to EXE here.
Learning how to convert a python file to an executable file is a very useful skill for any python developer or programmer.
There are many reasons why someone would have to convert a python file to an executable file. This seemingly simple but yet important task can help in many ways ranging from increasing the user-friendliness of a program to enabling a non-computer savvy person to be able to use and understand a script they made to help them automate a certain task.
In this article, we looked at the many different ways for a developer to convert .py files to .exe. These include using PyInstaller, cx_Freeze, and a GUI application called Auto PY to EXE.
With that being said, we hope you’ve found this guide helpful when it comes to making your python files executable in your development space.
Feel free to share this post with your fellow coders to guide them through converting their .py files to .exe!