Introduction:
Python is a popular programming language that is used for a wide range of applications, from web development to data analysis and machine learning.
One of the advantages of Python is its ease of use and portability, as Python code can be run on any platform that has a Python interpreter installed.
However, distributing Python applications to users who may not have Python installed can be challenging. One solution to this problem is to convert your Python code into a standalone executable file that can be run on any Windows computer without requiring Python to be installed.
In this blog post, we will explain how to convert a Python file into an executable (.exe) file and then how to package that file into a setup installer for Windows.
Features and Benefits:
Converting a Python file into an executable file has several benefits. One of the main benefits is that it makes your application easier to distribute to end-users who may not have Python installed on their computers.
An executable file can be run on any Windows computer, regardless of whether Python is installed or not. This makes it easier for users to install and run your application without having to worry about installing Python.
Another benefit of converting your Python code into an executable file is that it can help protect your code from being easily reverse-engineered or modified. By converting your code into an executable file, you can make it harder for others to view or modify your code.
Finally, packaging your executable file into a Windows setup installer makes it even easier to distribute your application to end-users. A setup installer can include all the necessary files and dependencies required for your application to run, as well as provide a user-friendly interface for users to install and configure your application.
How to do?
Steps to Convert Python File into Executable and Create a Windows Setup Installer:
Step 1: Install the PyInstaller Library
The first step in converting your Python code into an executable file is to install the PyInstaller library. PyInstaller is a popular library for converting Python code into standalone executables.
You can install PyInstaller using pip, the Python package manager, by running the following command:
pip install pyinstaller
Step 2: Create a Spec File
Once PyInstaller is installed, you need to create a spec file for your Python script. A spec file is a configuration file that tells PyInstaller how to package your Python script into an executable file.
To create a spec file, run the following command in your terminal or command prompt:
pyinstaller --name=myapp myfile.py
Replace `myapp` with the name you want to give your executable file, and `myfile.py` with the name of your Python script. This will create a spec file called `myapp.spec` in the same directory as your Python script.
Step 3: Modify the Spec File
Next, you need to modify the spec file to include any additional files or dependencies required by your Python script. This can include things like data files, image files, or other modules that your script depends on.
Open the spec file in a text editor and modify the `datas` and `hiddenimports` lists to include any additional files or modules required by your script. For example:
# myapp.spec
# ...
a = Analysis(['myfile.py'],
pathex=['/path/to/myfile'],
binaries=[],
datas=[('/path/to/data/file', 'data')],
hiddenimports=['my_module'])
# ...
```
In this example, we have added a data file located at `/path/to/data/file` and a module called `my_module` to the `datas` and `hiddenimports` lists, respectively.
Step 4: Build the Executable
Once the spec file has been modified, you can use PyInstaller to build the executable file. To do this, run the following command:
pyinstaller myapp.spec
This will create an executable file called `myapp.exe` in a `dist` directory in the same directory as your Python script.
Step 5: Create the Windows Setup Installer
Finally, you can use a third-party tool like Inno Setup or NSIS to create a Windows setup installer for your application. These tools allow you to create a user-friendly installer that includes your executable file, as well as any other required files or dependencies.
To create a setup installer using Inno Setup, follow these steps:
1. Download and install Inno Setup from the official website.
2. Open Inno Setup and create a new script.
3. Add a `[Files]` section to the script and include your executable file and any other required files or dependencies.
4. Add a `[Run]` section to the script and specify the command to run your executable file.
5. Build the setup installer by clicking the "Compile" button.
All Steps:
Step 0: Install pyinstaller by pip install pyinstaller
Step 1: Generate and get a .exe file and dist and build a folder
goto auto-py-to-exe
Script Location: Choose your main Python program file
Onefile: Choose "One File"
Console Window: Choose "Window Based (hide the console)"
Icon: Choose your icon.ico
Finally, you will see Current Command similar like
pyinstaller --noconfirm --onefile --windowed --icon "C:/Users/****/icon.ico" "C:/Users/****/main.py"
pyinstaller --noconfirm --onefile --windowed --icon "C:/Users/****/icon.ico" "C:/Users/****/main.py"
Copy the above code and make it like this and run it on terminal
pyinstaller --onefile --windowed --icon "C:/Users****/icon.ico" "C:/Users/****/main.py"
After successfully completed
You will get folders like build and dist and dist have mian.exe
.exe file in dist
and one more folder directory build
Step 2: Create a new folder copy your dist folder that has .exe file and build folder also .icon means your icon file in the same folder
Step 3: Download Inno Compiler and install
Step 4: Open Inno Compiler
Step 5: Start
File > new > Next
Give Application name like GUI App
Give an application version like 1.0
Give the application publisher name like IndianTechnoEra
Then give the application website thereafter
Next
Do not change anything in the Application folder section
Next
Here Application Files section
Browse the main .exe file that was created by auto-py-to-exe
Add the additional folder that was created new after generating the file and folder
Next
Application Shortcuts do not change anything
Next
Here in the Application Description section, we can add
License data and additional information
Next
The setup install mode can be changed or remain as it is
Next
Choose the language of the software
Next
Compiler Settings Section
Add your output folder destination
Name of the software setup like setup or main or whatever you like
Browser the software icon
Here it can be used also the set the password or leave
Next
Inno Setup Preprocessor section
Check on the box
Next > Next > Finish
Yes > Yes> Give the file name that will be like mySetupCompier.iss > Save
Wait for setup complete
After completing goto your folder where you chose as output
There you will get a new .exe file
Start installing
Double-click on the new.exe file
You will get the installer section
Choose the directory to install the application
Then Next
Check on the box for the desktop shortcut
Then Next > Next and Install
Your software installation will start
Then Finish
After installing the software we can use
We can search in Windows that it has been installed or not
Conclusion:
Converting your Python code into an executable file and packaging it into a Windows setup installer can make it easier to distribute your application to end-users.
By following the steps outlined in this blog post, you can create a standalone executable file that can be run on any Windows computerwithout requiring Python to be installed.
Additionally, packaging your application into a setup installer can provide a user-friendly interface for users to install and configure your application.
