VOOZH about

URL: https://thenewstack.io/how-to-create-a-python-executable-file-with-pyinstaller/

⇱ How to Create a Python Executable File With PyInstaller - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-07-06 03:00:30
How to Create a Python Executable File With PyInstaller
tutorial,
Python / Software Development

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 Jack Wallen
👁 Featued image for: How to Create a Python Executable File With PyInstaller
You’ve spent the time creating a Python app that you want to make use of or you want to distribute it to people who could benefit from its awesomeness. You might think the only way to do that is to send the code to them, make sure they have Python installed on their machine (and any dependencies required for the code), and instruct them to run the code with the command python3 appname.py Sure, that would work, but it’s not exactly efficient. And if you’re sharing an app with someone who might not exactly know the ins and outs of Python, that can be problematic. Or, even if they do know their way around Python, you certainly don’t want to have to send them your code and expect them to run it from the command line. Instead, why not create an executable file from your Python code, so all the user has to do is either double-click on it to run the app, or copy the file into a directory in their $PATH and run the command from anywhere in the filesystem hierarchy. That’s what I’m going to show you how to do today. We’ll use the code from our previously created Python app (for taking a user’s input and writing it to a file with the help of a GUI) and create a handy executable from it. This is a great way to create a portable app from your Python code. The only requirement a destination machine will need is to have Python installed (which is a fairly simple hurdle to overcome). Right. Let’s get to the process.

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_())
Let’s create a new directory with: mkdir INPUT_APP Change into that directory with the command: cd INPUT_APP Create the Python file with: nano input.py Paste the code above into that file and then save/close it. You can then create the bundled folder with the command: pyinstaller input.py When the command finishes, change in the dist directory with: . In this folder, you’ll find a sub-folder named input, which contains the executable, and a folder named _internal. Change into the input folder with cd input and you can then run your app with the command: ./input The input GUI will open and you can use the app. If you want to distribute the app like this, you’d copy the INPUT_APP/dist/input folder to whoever needed it and they could run it the same way you did (as long as they had Python installed on their machine). There’s an easier way.

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.
TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.