Back to articles list Articles
7 minutes read

How to Make a Python Program and Send It to Someone: A Beginner's Guide

Have you ever wondered how you can transform your Python code into a standalone application that anyone can run and use? Learn how to make and share Python executables in this article.

Your code is stored in a Python script, but this doesn’t mean you can share it with friends and family as a ready-to-run application. In this article, we’ll talk about creating a Python executable and using it to create standalone applications that you can share with others.

If you're new to programming or need a quick refresher on its basics, consider checking out our Python Basics Course before we embark on this distribution adventure. This set of three interactive courses will take you about 38 hours to complete and will teach you fundamental programming concepts with Python. By the end of it, you will be able to write your own reusable code using functions.

Once you're armed with the fundamentals, you'll be better equipped to wield the tools and concepts we're about to explore. First, let’s talk about what the Python executable is and what it does.

TL;DR: Quickly Creating a Python Executable

A Python file requires the Python development environment (as well as program-specific dependencies) to run. If you want to share your work, your recipient has to have a Python environment (including those specific dependencies) and know how to load and run that file.

A Python executable bundles in these dependencies and allows you (or anyone else) to run the file outside of the Python environment. Fortunately, PyInstaller (and similar programs) package Python code files with all their dependencies into an executable file. If you have PyInstaller installed, you can quickly share a Python program by running Python -m PyInstaller --onefile your_script.py in your command line tool of choice.

However, there are a lot of important things we’ve skipped in this TL;DR version. Let’s go back to the beginning and talk about all the parts you need to make a Python file into an executable file that will run on its own.

First, let’s quickly review key Python terms.

Know Your Python Terminology

Source Code

The source code is the human-readable Python (or any other programming language) code that developers write.

Interpreter

The Python interpreter plays a fundamental role in executing Python code by translating human-readable source code into machine-understandable instructions.

Bytecode

The Python interpreter translates this source code into an intermediate form known as bytecode. Bytecode is a set of low-level instructions that are not specific to any particular machine architecture.

Python Virtual Machine (PVM)

The PVM acts as a runtime engine that interprets and executes the instructions in the bytecode, ensuring the correct behavior of the Python program.

Distribution Packages

Distribution packages in Python are collections of modules, scripts, and data files that are bundled together for easy distribution and installation. They are organized in a specific format to simplify the process of sharing and installing Python software.

Libraries

In Python, a library refers to a collection of modules or packages that provide reusable code and functionality. Libraries are designed to address specific domains or tasks, making it easier for developers to incorporate powerful features into their applications without reinventing the wheel.

The Python Interpreter: Bridging Code and Execution

The Python interpreter serves as the bridge between your human-readable source code and its machine-understandable execution. When you run a Python script, the interpreter takes the source code, converts it into bytecode, and executes it using the Python Virtual Machine (PVM).

However, sharing Python source code also implies that your user has Python installed and knows how to run a Python script. Now, imagine the convenience of sharing your Python masterpiece without worrying about whether the recipient has Python installed. That's where executables come into play.

Executables encapsulate your script, the interpreter, and any dependencies into a single file. This means your code can run independently, regardless of whether Python is installed on the user's system.

Creating an executable will allow us to bridge this gap. The result? Your Python program becomes self-contained, ready to run on various systems without requiring users to install Python separately.

This is important for several reasons.

First up is portability. Python executables make your code portable, allowing it to run seamlessly on different platforms without users having to think about Python.

Second, and directly connected to portability, is ease of sharing. You can now share your programs effortlessly, sparing others the need to install Python and any dependencies.

Finally, this makes it user-friendly and easy to run the executable.

At the end of the day, understanding the transition from the Python interpreter to Python executables opens new doors for sharing and distributing your creations.

So, are you ready to take the next step? Let's delve into the practicalities of creating a standalone executable from your Python project, opening up a world of possibilities for developers and end-users. Let's get our hands dirty!

Sharing a Python Program in Practice

Creating a single executable is a convenient option for beginners looking to share their Python programs. Among the choices available, PyInstaller is an easy-to-use and well-documented tool for generating executables across different platforms.

Now, let's go step-by-step and create a very simple Python script that we will turn into an executable with PyInstaller. If you are on Linux, Python comes out of the box. Otherwise, you will need to install Python on Windows.

Let's create a simple Python application that you can share with others.

First, we need a Python script that we will convert into an executable that you can share with your friends. It could be anything, such as a game or some utilities. However, since we only want to illustrate the concept of using Pyinstaller, we will create a script called hello_lpy.py that prints "Welcome to LearnPython.com".

Here is the code:

def main():
    print("Welcome to LearnPython.com!")


if __name__ == '__main__':
    main()

In the snippet above, we run a main function. Having such a function in this Python script is unnecessary because the Python interpreter runs the code from top to bottom unless a specific function is specified. But it is good practice to use functions and classes whenever we can.

Now that we have a script to convert into an executable, we need to install PyInstaller, which can be done straightforwardly using pip.

pip install pyinstaller

Next, navigate to your Python script directory and use PyInstaller to create the executable. This is done by providing the entry-point file as an argument. Here, we will use the onefile flag to have only one file as an executable.

Go to your script's directory and convert your Python script into an executable.

cd path/to/your/script

python -m PyInstaller hello_lpy.py

After executing the command, you will have a build and a dist folder. You can leave the build folder aside for now. The executable you want to share with your users will be found in the dist directory within your project folder.

If you are on Windows, you will have a hello_lpy.exe file. It is essential to note that PyInstaller will create an executable specific to the environment that you are using, whether it is Windows, Mac OS, or Linux. However, PyInstaller is not cross-platform because it cannot create executables for all three operating systems simultaneously or as specified.

To run the executable, open a command terminal in your folder and type the following command:

.\hello_lpy.exe

You’ll see the following output:

Welcome to LearnPython.com!

Congratulations! You have just created your first executable. You are now free to share your apps and scripts with your friends.

Other Python Executable Scenarios

PyInstaller and the like are not the only way to deliver Python software. Developing Python web applications using frameworks such as Django or Flask is also possible.

We can also use libraries like TensorFlow or scikit-learn to train machine learning models that can later be embedded into software or web applications. We can even create mobile apps with Kivy.

Sharing Python Executables: What's Next?

In this article, we bridged the gap between the Python developer and the end-user by learning how to create an executable with PyInstaller.

Why not try to create something more complex and share it with others to show your progress? And why not use your Python skills to improve their lives? That's the true power of programming, after all.

If you haven't started your programming journey and are wondering if you should, dive into our Python Basics track. Soon, you’ll be able to build a Python executable with what you've learned!

See you soon on LearnPython.com!