How to create a Python executable file using PyInstaller

Python is a dynamic high-level programming language popular for its easy and readable code structure. Built-in libraries and functions make it the go-to language for developers looking for rapid application development.

PyInstaller is a Python library that bundles a Python program into a standalone executable package that runs without installing Python or any modules associated with the program. In this answer, we will see how to install PyInstaller and convert the Python file into an executable one.

Install PyInstaller

Let's install PyInstaller in the same directory where the Python file (e.g., main.py) is present using the following command.

pip install pyinstaller

Here, we use pip which is a package manager for Python.

Create an executable Python file

To create an executable Python file, we use the following command that creates two folders (dist and build) along with a .spec file, and the resulting executable file resides in the dist folder.

pyinstaller --onefile main.py
  • pyinstaller invokes the PyInstaller package.

  • --onefile is a flag that tells the PyInstaller package to create only a single executable file instead of multiple files.

  • main.py specifies the entry point of the project that needs to be converted to an executable file.

Code example

Let's run the following terminal and see the aforementioned commands in action.

Terminal 1
Terminal
Loading...

As mentioned above, the executable file is created in the dist folder. That's why after creating the executable file, we navigate to dist folder using cd command and run ./main command to execute the file and print the current time.

Conclusion

The PyInstaller package helps us to create the executable file of Python file. It bundles all the libraries and packages into a single .exe file that we can run without explicitly importing packages and libraries.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved