How to Convert Python to Exe file?
Though there are different options available to convert Python to Exe file, this article explain about one easy option. This package compile Python to executable. Also includes the additional libraries the py code is dependent on.
This way, the target computer does not need to install Python or required libraries. This compiling package is available for Windows, Max & Linux.
But it does not create a cross platform enabled executable file as mentioned in this official page. Meaning, you have to compile the code with this package in each environment. i.e., Install the package in Windows to get a exe for windows, Install & compile in Linux to get a corresponding Py to exe.
Note: It is assumed that you have the latest Python installed. This methods is tested & works fine in Windows 10 PC.
1. Compile Python to Exe
Usually, there are multiple pre installation steps need to be done with other Python compilers. Like preparing a configuration file, specifying the dependent libraries etc., But with this installer, its like a jump start.
Package we are going to use to make Python exe is called PyInstaller. Follow these steps to convert a Py ccode into an executable file.
- In Cmd type ‘pip install pyinstaller’
- Go to python program (.py file) folder
- Shift + Right click inside folder.
- Choose ‘Open PowerShell Window here’
- Type these commands in PowerShell:
C:\Folder> pyinstaller --onefile 'myPythonProgramName.py'
Wait for few seconds while the program gets compiled.
This process will create 3 folder. We need only the ‘dist’ folder. This folder will have myPythonProgram.exe which can be distributed to your client & hence the name dist.
You have to copy the whole folder or create a zip if you would like to distribute to this to multiple computers.
Issues while Exe creation: Follow this link to resolve any issues you encoutner during a executable file creation. Click here
2. Cross Platform Python Compiler – Windows/Mac/Linux
PyInstaller is available for multiple OS platforms. But the resulting exe is not cross platform.
This means: If you want to run a Python exe in a Windows PC, it has to be compiled in Windows PC. If target machine is Linux, then create exe in a Linux machine. And same for MAC or any other OS.
Using the steps above, the whole process takes 2 to 5 mins. Python has been used in a variety of environments due to the availability of numerous libraries. So, this kind of compilers will enable more users to like this wonderful programming language.
3. Py to Exe
If you dont copy the whole dist folder, and copy only the exe file to other Pc, then you have to install all the dependent libraries in the target machine.
The whole purpose of dist folder is that it will have all the libraries that you have mentioned in “Import” function within the python program. Otherwise, you have to install Python package and then install individual libraries separately in target machine.
PyInstaller is a light weight quick compiler. This does not produce a Installer kind of package, which makes it less preferred for heavy Python applications.
But if you are just porting a simple to medium Python application, then this package would just serve the purpose and with just minimal steps.
Related Topics that might interest you: