Dice grid from a photo with Python (V) - Creation of a stand-alone executable


This post is part of the Project: Create a grid of dice from a photo with Python and PIL

Introduction

Up to this point, we've designed the program and created the modules and the GUI coding with Python. We could now say that the program is complete! But we can still work on one last thing: The creation of a stand-alone executable that will help us to distribute our program, as it will be able to be run on any machine even without a Python interpreter installed.

For this, we'll use Pyinstaller. With pyinstaller you can create an executable file for your operative system (i.e. you can not create an EXE for Windows running pyinstaller on Ubuntu, to do that you'd need to run it on Windows). 

Downloading Pyinstaller

Pyinstaller can be added to your machine through PIP with the following command:

pip install pyinstaller

Remember that some Linux distros refer to PIP and Python 3.x as pip3 and python3 instead of pip and python, so you might need to change the previous command to fit your configuration.

Creation of the stand-alone executable on Windows

To create an executable using pyinstaller you only need to type the following command:
pyinstaller program.py

This will create a directory that will store all the dependencies and libraries needed by the program to run. However, you can also export your program to a single file executable adding the flag --onefile:
pyinstaller --onefile program.py

Although pyinstaller is very good on its own to find all the dependencies of the program, it doesn't find them all everytime. If pyinstaller doesn't find a module of the program we can add it manually through the option --hiden-import.
In my case I had to import the module PIL._tkinter_finder when using Pyinstaller on Ubuntu 18.04:
pyinstaller --onefile gui.py --hiden-import='PIL._tkinter_finder'

The output will be an executable file named gui with no extension (on Linux) or an EXE file on Windows.
Remember that if you are using any relative paths to any file in your program you'll need to keep those in the same folder as the executable, or you can replace any relative paths with absolute paths.
For example, in gui.py, I had to change the path to the icon file in set_icon() from a relative path to an absolute path:


And that's all! Can you think of how can you enhance this program? Without thinking so much, there are some that come to my mind:
  • Making the dice resizable
  • Adding a more accurate preview
  • Adding multiple dice styles
  • Making the window resizable/responsive
  • Adding multithreading to avoid "Not responding" warnings
  • Making this a batch tool
  • Repurposing the program as a web app using Django or Flask
However, each of these ideas could need a tutorial on their own...
Anyway, always keep learning and share your ideas with the world!
See you in the next post.

Comments

Popular posts from this blog

How to install Spyder 3 on Windows without Anaconda

How to install PyQt5 and Qt Designer on Ubuntu

Hello World in Python 3