How to Convert PY File to EXE

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

 

 

 

Installation 

First you need to install pyinstaller, you can use pip for the installation.

 

 

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.

 

 

Now copy this file and paste that in the Scripts folder of your Python Installation. 

Python Scripts Folder
Python Scripts Folder

 

 

 

After that open your terminal in the Scripts folder and run this command.

 

 

 

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.

Python scripts Folder
Python scripts Folder

 

 

 

 

 

 

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.

 

 

 

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.

 

 

 

 

The code is converted to EXE, and you can find the exe in the dist folder.

Convert Python PY File to EXE File with PyInstaller
Convert Python PY File to EXE File with PyInstaller

 

 

 

 

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.

Python Inno Setup
Python Inno Setup

 

 

 

 

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.

Python InnoSetup Installer
Python InnoSetup 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.

Inno Setup Add Folders
Inno Setup Add 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.

Inno Setup Application Shortcut
Inno Setup Application Shortcut

 

 

 

 

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.

Inno Setup Application Documentation
Inno Setup Application Documentation

 

 

 

 

Don’t bring any change in the setup install mode page click on next.

Inno Setup Install Mode
Inno Setup Install Mode

 

 

 

 

After that choose your language and click on next, now you need to give the compiler output folder also add your icon in here.

Inno Setup Compiler Setting
Inno Setup Compiler Setting

 

 

 

 

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

 

3 thoughts on “How to Convert PY File to EXE”

Leave a Comment

Share via
Copy link
Powered by Social Snap
×