How to Create a Python Executable File With PyInstaller
This is a great way to create a portable app from your Python code.
Jul 6th, 2024 3:00am by
What You’ll Need
To make this work, you’ll need a machine with Python installed and the sample code we created last time around. I’ll add the code here, so you don’t have to search for it. I’ll demonstrate this on Ubuntu 22.04 but the process will work on any Linux distribution (or any OS that supports Python). If you’re using a different distribution or operation system, you’ll need to adapt the Pip installation process accordingly.Installing Pip
To install PyInstaller, you must first make sure Pip (the Python package manager) is installed. You can check to see if Pip is installed with the command: pip –version If you see the version number printed in the console, you’re good to go. If you get an error, you’ll need to install Pip, which is done with the command: sudo apt-get install python3-pip -y When the above command completes, you’re ready to continue.Installing PyInstaller
Next, we need to install PyInstaller, which reads your Python code, discovers every module and library your app needs to run, collects everything necessary (including the Python interpreter), and combines them with your code into a single folder or a single, executable file. To install PyInstaller, issue the following command: pip install pyinstaller That’s it. You’re ready to go.Bundling Everything Together
The first thing I’ll do is show you how to use PyInstaller to create a bundle for your app. This will all be housed in a folder that includes an executable file and a folder containing the dependencies. Remember our code for the input GUI looks like this:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout
class UserInputApp(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle('User Input App')
self.setGeometry(100, 100, 400, 200)
self.label = QLabel('Enter text:')
self.text_input = QLineEdit()
self.save_button = QPushButton('Save to File')
self.save_button.clicked.connect(self.save_to_file)
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.text_input)
layout.addWidget(self.save_button)
self.setLayout(layout)
def save_to_file(self):
text = self.text_input.text()
with open('user_input.txt', 'a+') as file:
file.write(text + '\n')
print('Text saved to file.')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = UserInputApp()
window.show()
sys.exit(app.exec_())
Creating a Single File Executable
The best way to do this is to use PyInstaller to create a single file executable file. The only difference here is the command you run (within the INPUT_APP folder), which is: pyinstaller –noconsole –onefile input.py The –noconsole option instructs PyInstaller to suppress the terminal window that will inevitably open with the app and the –onefile tells PyInstaller to create a single file executable. When this command completes, you’ll find the single file executable in the dist/input directory. You can then copy that file to a directory in your $PATH (such as /usr/local/bin) or copy it to anyone who needs the app. And that’s all there is to creating an executable file from your Python code. With this handy method, your app is not only easier to run but easier to distribute to other users.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.