Convert Python Script to Exe Using Pyinstaller
“How to convert Python Script to .exe file?” – Every python developer asks this question when they want to share their developed python application to the outer world. In this blog, we will take a detailed view on how to convert our python program to an executable file using a python module i.e pyinstaller.
Why can’t share the .py file?
While developing in python, we install many packages and use them in our program. Those installed packages are present in our development environment.
When we share our python file to another machine, for that python program to run on that machine the same packages should be present.
One easy way to install all the packages is to use the requirements.txt file which has all the list of installed packages needed for that python program to run.
Fill the requirements.txt file
This command should be executed in the development environment of the python developer.
$ pip freeze > requirements.txt
Install Packages from Requirements.txt file
This command should be executed on the machine where the python packages are to be installed to run the shared .py file.
$ pip install -r requirements.txt
But what if, the developer doesn’t want to share the requirenments.txt file and still wants to run that developed application on another machine.
This problem can be solved when we convert the python script file (.py) to an executable file (.exe).
The obtained executable file can run on the windows platform and it contains all the packages needed to run the python program.
What is Pyinstaller?
Pyinstaller is a python package that bundles a Python application and all its modules/dependencies into a single package.
This makes tasks easier for the user, as the user can run the bundled app without installing a Python interpreter or any module on the machine.
Pyinstaller also supports many python libraries like NumPy, PyQt, Django, wxPython, and others.
Install Pyinstaller using Pip
In order to convert the python file to a .exe file, we will use a python package i.e pyinstaller.
$ pip install pyinstaller
This will install the pyinstaller on the machine and we can use it to convert the python program to the .exe file.
Convert Python to Exe File
pyinstaller [options] script [script …] | specfile
Open the terminal and change the directory where your ‘.py’ file is located and execute the command given below.
$ pyinstaller yourprogram.py
Let’s see what the above command performs:
yourprogram.specin the same directory as the script.
- Creates a folder build in the same folder as the script if it does not exist.
- Writes log files and working files in the build folder.
- Creates a folder dist in the same folder as the script if it does not exist.
- Writes the
yourprogramexecutable folder in the dist folder.
The dist folder contains the bundled app and an executable (.exe) file inside the bundled app folder.
When you will run the .exe file, you will get the same output as you get when running it from the python interpreter.
One problem here is that still you cannot share the .exe file alone, as there as a lot of dependency files that the .exe file needs in order to run.
Pyinstaller provides us with various flags that help us to modify our bundled app.
Pyinstaller OneFile flag to convert python file to .exe
$ pyinstaller --onefile yourprogram.py
This command will create an executable file in the dist folder without any dependencies, as all the dependency files are bundled in the .exe file because of the –onefile flag.
If your python program is a GUI application, you would have noticed that a console window is opened at first and after a few seconds the GUI application starts. This is not good for the User Experience.
This problem can be solved by using the -w / –windowed flag in the pyinstaller.
$ pyinstaller --onefile -w yourprogram.py
Set Pyinstaller Icon for .exe
By default, Pyinstaller will give a python look to the obtained executable file. In order to change the default icon of the executable file, you will have to set the -i / –icon flag.
The -i / –icon flag takes the path to the .ico icon image file.
$ pyinstaller --onefile -w --icon=images/app.ico yourprogram.py
This command will create a standalone executable file with your custom icon.
Learn Intermediate Python.
Learn GUI Programming in C.
Learn more about how to convert python to exe file and pyinstaller flags and commands from the official documentation.