In this article i want to show you that How to Convert PY File to EXE , for converting Python PY File to EXE File we can use pyinstaller library, PyInstaller bundles a Python application and all its dependencies into a single package. The user can run the packaged app without installing a Python interpreter or any modules. also we will learn that how you can create setup installer for your EXE File. for creating setup installer we want to use Inno Setup software.
Learn More
- How to Download YouTube Videos in Python
- Python Google Translate API
- How to Convert Text to Speech in Python
Installation
First you need to install pyinstaller, you can use pip for the installation.
1 |
pip install pyinstaller |
OK now we want to create two examples, the first one is just a basic python hello world code, and the second one is a simple gui application in PyQt5.
Create a python file at name of hello.py and just we are printing a simple hello world in that file.
1 |
print("Hello World") |
Now copy this file and paste that in the Scripts folder of your Python Installation.
After that open your terminal in the Scripts folder and run this command.
1 |
pyinstaller --onefile --windowed hello.py |
What to generate Option
-D, --onedir | Create a one-folder bundle containing an executable (default) |
-F, --onefile | Create a one-file bundled executable. |
--specpath DIR | Folder to store the generated spec file (default: current directory) |
-n NAME, --name NAME | |
Name to assign to the bundled app and spec file (default: first script’s basename) |
Windows and Mac OS X specific options
-c, --console, --nowindowed | |
Open a console window for standard i/o (default). On Windows this option will have no effect if the first script is a ‘.pyw’ file. | |
-w, --windowed, --noconsole | |
Windows and Mac OS X: do not provide a console window for standard i/o. On Mac OS X this also triggers building an OS X .app bundle. On Windows this option will be set if the first script is a ‘.pyw’ file. This option is ignored in *NIX systems. | |
-i <FILE.ico or FILE.exe,ID or FILE.icns>, --icon <FILE.ico or FILE.exe,ID or FILE.icns> | |
FILE.ico: apply that icon to a Windows executable. FILE.exe,ID, extract the icon with ID from an exe. FILE.icns: apply the icon to the .app bundle on Mac OS X |
After that you will have two directory in your Scripts folder, build and also dist, your exe is located in to dist folder.
- How to Load Image In Android Studio
- How to Create Custom Spinner in Android Studio
- How to Create Notification Badge in Android Studio
- How to Convert Website to Android Application
Converting PyQt5 GUI to EXE
Now we are going to convert our PyQt5 GUI application to exe and after that we create a simple installer for the gui application. the process is the same above just create your PyQt5 code, so PyQt5 is set of Python bindings for the popular Qt application development framework. it allows Python developers to create cross platform graphical user interfaces (GUIs) using Qt tools and libraries.
Qt is C++ framework that provides comprehensive set of tools for building GUI applications. It includes different graphical widgets, such as buttons, labels, text boxes and tables, as well as support for handling events, creating layouts, and handling input from the user. Qt also includes tools for creating 2D and 3D graphics, multimedia applications and many more, in the below code we are going to build simple Python GUI Window with PyQt5 and after that we are going to convert that to EXE file and make installer for that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel import sys from PyQt5.QtGui import QIcon, QFont class WindowExample(QWidget): def __init__(self): super().__init__() self.setGeometry(200,200, 400,300) self.setWindowTitle("Codeloop.org") self.setWindowIcon(QIcon('python.ico')) self.setStyleSheet('background-color:red') vbox = QVBoxLayout() label = QLabel("Please Subscribe My Channel") label.setFont(QFont("Sanserif", 14)) label.setStyleSheet('color:white') vbox.addWidget(label) self.setLayout(vbox) app = QApplication(sys.argv) window = WindowExample() window.show() sys.exit(app.exec_()) |
Now copy this file and also one icon to the Scripts folder, make sure that the icon should be .ico extension, run this command in the terminal of Scripts folder.
1 |
pyinstaller --onefile --windowed --icon=python.ico window.py |
The code is converted to EXE, and you can find the exe in the dist folder.
Make Installer For EXE File
You have created your GUI Application in PyQt5 or Tkinter or any GUI library, after that you have converted python py file to exe file and now you want to make installer for your application, for this you need to download and install Inno Setup software.
Now make a folder and copy your two directories of dist and build, paste that in your created folder. also copy your icon in that folder like this.
Now open your InnoSetup, from File choose New and after that you need to add your application information that you want to make setup installer.
After that click on next, there will be another page of Application Folder, you don’t need to bring changes in their, just click on next. Now we have Application Files page, in here in the Application main executable you need to Browse your exe file from the folder that you have already copied. after that click on the Add Folder and you need to add your main folder that includes the dist and build folders.
Than click on next you will see Application Shortcut page, you don’t need to bring any changes in their, just click on next.
after that there will be Application Documentation, like you can add license and before installation instructions for the application, iam not going to give any documentation for the application so just click on next if you don’t want.
Don’t bring any change in the setup install mode page click on next.
After that choose your language and click on next, now you need to give the compiler output folder also add your icon in here.
Click on next and finish, now you are done and you have a setup for your application, you can install that on your computer and you can give the installer for your friends, they install the application and use from that 😀 .
FAQs:
Q: How do I convert a .py file to an EXE?
A: You can convert a .py file to an EXE (executable) file using tools like PyInstaller, cx_Freeze, py2exe, or Py2App. These tools package your Python script along with its dependencies into a standalone executable file that can be run on compatible systems.
Q: How do I change a file to EXE?
A: To change a Python file (.py) to an executable file (.exe), you need to use a tool like PyInstaller or cx_Freeze. These tools automate the process of bundling your Python script with its dependencies into a standalone executable file that can be executed on systems without Python installed.
Q: How do I run a .py file in Python EXE?
A: Once you have converted .py file to an executable (.exe) file, you can run it by simply double clicking on the executable file. Also you can run it from the command line by navigating to the directory containing the executable file and typing its name followed by any necessary command line arguments.
Q: How do I convert a Python program to an app?
A: Converting Python program to an app typically involves creating a standalone executable file (.exe for Windows, .app for macOS, or a Linux executable) using tools like PyInstaller, cx_Freeze, py2exe, or Py2App. These tools bundle your Python script along with its dependencies into an application package that can be distributed and run on compatible systems. Also if you want to create a graphical user interface (GUI) for your Python program, you can use GUI frameworks like Tkinter, PyQt, or PySide to develop the interface.
Subscribe and Get Free Video Courses & Articles in your Email
I didn’t know this was possible. Thank you very much 🙂
i thought it very difficult to covert it to exe or setup file.
thank you very much for explanation
if i have pictures attached in python code.
how can i show them inside exe file.