top of page
Writer's pictureBraulio Madrid

How to create executables in Python with pyinstaller



In this post have passed various topics from the game development to application programming with #tkinter, even terminal applications and there is still more topics to be discussed in this blog as i experience with different modules.


We suppose that we have created an application that we want share with a friend or with community, in this current state would have that say to our fríen that not is programmer, open terminal, install python, install modules with pip and share a list of dependencies and execute. The solution is creating an executable application in one file.


Into packages of python exist various alternatives for compile our python code in executables.


In Windows exist:

  • Auto py to exe $pip install auto-py-to-exe.

  • py2exe $pip install py2exe

  • pyinstaller $pip install pyinstaller

  • cx_freeze $pip install cx-freeze

In Linux exist:

  • pyinstaller $pip install pyinstaller (multiplataforma y el que usaremos aquí)

  • Buildozer $pip install buildozer (es multiplataforma, pero se usa para crear aplicaciones de android con interfaz kivy)

  • cx-freeze $pip install cx-freeze (multiplataforma)

In my case i test install in #ZorinOS and didn't have any problem, but if you have it, read carefully the necessary dependencies and install with pip command.


For this example, we do two codes very simple, a hello world in terminal and hello world with graphic interface using tkinter.


# holamundo.py
def main():
    print('Hola mundo')
    
if __name__ == '__main__':
    main()

# holaGUI.py
    
import tkinter

class Application(tk.Frame):
    def __init__(self,parent):
        super().__init__(parent)
        tk.Label(parent,text = 'Hola mundo!').pack()

def main():
    root = tk.Tk()
    root.title('Hola mundo aplicacion')
    application = Application(root)
    root.mainloop()

if __name__ == '__main__':
    main()    

The easiest way to make an executable with #pyinstaller, go to the folder were found the holamundo.py script, open a terminal and write the next command.

$pyinstaller holamundo.py

This command will create two folders, build and dist, open dist folder and find executable file with the same name without extension in Linux, in windows you will see the *.exe extension.


Like holamundo.py is a terminal application, you must execute from the terminal to work.


If you want compile the executable in one file, you must execute the next command.

$pyinstaller --onefile holamundo.py

Now let's go to case with graphic interface, when test the code, it's open two windows, the first contains the graphical interface y the other contain the terminal to debug errors, but compile time we want the graphical interface only, for this we compile with next command.

$pyinstaller --onefile --windowed holaGUI.py

With this instruction we have compiling in one file and windowed mode. If you want a personalized icon, write:

$ pyinstaller --icon = ./path/icon.ico holaGUI.py

The icon must be in the same folder with extension *.ico.


Note: In any distros like Debian, the pyinstaller command don't work, other way to use is executing the module directly:

$python3 -m PyInstaller holamundo.py

You should know some things, i mentioned the word "compile" many times, but it's not true, this simply pack our code and execute directly with the python interpreter without us realizing, don't expect extra performance.


If you have any problem to compile, check that not use help() and exit() or any reflexive function in your python code, this damage the compilation.


not all dependencies are supported, for to know that packages are compatible review the next link.


I hope this has been helpful, there is nothing more that makes you feel like a programmer like seeing the executables of your programs running without weird witchcraft. Not being more see you in a next post.


2 views0 comments

Recent Posts

See All

コメント


bottom of page