Python Apps

Posted on Thu 27 February 2020 in Python

Creating Python Apps

Suppose you have a script that for instance runs visualizations or generates a report and that has to be shared to non-programmers users to be executed regularly. Even if you create a container that includes all the program dependencies there is still a chance that the final user can get lost in how to use it.

Fortunately, any user who has a basic concept on how to install and run apps would be able to run your app if you provide them an executable file. There are different ways to create an executable app but in this post we will show a way that works in any platform and it's relatively easy to work with; we will be using PyInstaller and will refer to Python 3 along the post.

What is PyInstaller and how to install it?

PyInstaller is a python library that freezes Python applications into stand alone executables under almost any OS. It allows you to create apps that won't require the final user to install a specific Python version and or packages to run the app because the executable would contain all what the app needs to run (to read more about the PyInstaller project click here.

To install it first you need to make sure you have a dev version of python, that includes, libpython3.x.dylib and/or Python soft link among other options.

Windows

If you are running python in Windows you don't have to worry about doing an extra step, you just need to install it via:

pip install PyInstaller

Mac OS X

It is highly recommended that you have python installed using a package administrator like Homebrew, since PyInstaller works with the default Python 2 provided by Mac OS X installations (see How to install PyInstaller)).

A hassle free option is to install the dev version via pyenv enabling the framework using (for more info check the official documentation):

env PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.x-dev

Then point the global python version to the one just build with:

pyenv global 

If everything went fine, you should have something similar to this:

pyenv versions
> system  
>  3.7.4  
>* 3.8-dev (set by PYENV_VERSION environment variable)  
>  3.8.2  
>  xz-5.2.4  

To install pyinstaller run:

pip install pyinstaller

Creating an App

The developers of pyinstaller have made a great job keeping the app development very simple and straight forward. We will cover the two most common ways to use it but if there are special requirements that are not cover here please refer to the official documentation.

Quick one

If you have installed a dev version, make sure that all the external libraries (packages) that are used in you app are also installed for the dev version before running pyinstaller, this will ensure that all the dependencies are included in the final app. As of writing this post I have encountered issues when a single function is extracted from an external library, hence I personally recommend to include the whole library in your script (import , instead of from import ).

In the console (command line) go to your project, example '~/projects/myappproject' and run:

pyinstaller myapp.py

This will create a distribution folder '~/projects/myappproject/dist' where the app will be stored. To try your app, simply go to '~/projects/myappproject/dist/myapp' and look for the executable file and run it.

Adding Data

In case the app has dependencies on other files, a simple way is to keep them all in a single folder and run in Windows:

pyinstaller --add-data "dependencies/*;dependencies/" myapp.py

Mac OS X:

pyinstaller --add-data "dependencies/*:dependencies/" myapp.py

This will ensure the files are included in the distribution.