Python Pyinstaller Tutorial

This tutorial will cover the python library pyinstaller and how to use it.

Before we get into the inner workings of pyinstaller, we’ll have a brief discussion on software distribution. This section is optional and you can skip ahead to the next section if you want.


Software Distribution

There’s a 9 out of 10 chance you’re here because you’re looking for a way to protect your source code while also distributing your precious software to clients and customers. Piracy is an issue that has plagued distributors and developers for years, and it isn’t about to stop any time soon. So how is pyinstaller supposed to help us here?

Normally, you might think to distribute your software, that you have to give someone your source code. Technically yes, you can do it that way. But giving someone your source code allows him to further distribute and modify your software without your consent. (Without you getting any profit out of it)

This is where compilers like pyinstaller come in. Compilers translate source code from a high-level programming language to a lower level language to create an executable program. This compiled program can then be distributed safely as it’s not the source code.


Pyinstaller Setup

Python doesn’t natively come with a way to create compiled programs. However, pyinstaller is a python library that can be used for this purpose. You install it as you would a regular library using the pip install command as shown below.

pip install pyinstaller

Check our getting started with Python guide if you’re having trouble installing the library.


Understanding Dependencies

Before we move forward you should understand the concept of dependencies in Python. Before using Python on a device, what is an essential step you need to do before this? Install python of course. But what’s to say that your customers/users will have python installed? Those 3rd party libraries you downloaded and installed for your program aren’t included with your source code. We call such requirements, “dependencies”.

The whole point of a compiled program is that it can run without the need for the user to install any additional software before using your program. So how does it handle this? It compiles along with your code, any libraries that you used and allows it to run on a computer which doesn’t have a python interpreter installed. The end result is that the compiled file is much larger than the source code. The more dependencies your source code has, the larger the end file size.

Knowing how dependencies work isn’t required to use this library, but it’s good to know the concepts upon which pyinstaller is based. Furthermore, this knowledge comes in handy if something goes wrong, as you’ll see further on.


Using pyinstaller in Python

Now that you’re ready, go to the command prompt, and navigate to the file you wish to convert. In this case, we’ll be converting a file called “TestFile.py“. First we will navigate to the directory where our File is stored. In our case the file is located in the Desktop.