Freezing your Python code

By Sambhav Kothari

Elevator Pitch

After all the wrangling with the snakes, pushing your way through scary octocats, and even slaying the mighty review beasts, you present the fruits of your labor to the King (your users). Then, on their first bite, they choke on a “pip”… what now?

Description

Although Python makes it extremely easy to write your packages and applications, it leaves much to be desired when it comes to actually distributing this code to end-users. Not everyone is familiar with pip, virtual environments, wheels and PyPi. Incompatible and unresolved dependencies might lead your users to frustration and to you pulling your hair out trying to debug hundreds of possible environmental combinations that might cause your application to fail. This becomes even harder when your users don’t have the “right” version of Python installed or, in a worst case scenario, they are non-developers running Windows who haven’t even heard of Python.

“Freezing” your Python code is one way out of this conundrum. Never heard of “Freezing”? From the Hitchhiker’s Guide to Python -

“Freezing” your code is creating a single-file executable file to distribute to end-users, that contains all of your application code as well as the Python interpreter.

Applications such as ‘Dropbox’, ‘Eve Online’, ‘Civilization IV’, and BitTorrent clients do this. The advantage of distributing this way is that your application will “just work”, even if the user doesn’t already have the required version of Python (or any) installed.

In this talk, I will give an introduction to freezing your Python code using PyInstaller. By the end of this talk, attendees will be able to create stand-alone executables that are able to run on Windows, macOS and Linux distributions – free of any dependencies. I will also provide reusable code-snippets and recipes for continuous delivery of your applications to all three platforms using free tools like Travis CI and AppVeyor.

Notes

Technical Requirements

This talk is aimed at intermediate Python developers and assumes that the attendees have basic familiarity with:

  • Python packaging
  • Related tools like pip
  • Git/GitHub
  • CI/CD platforms like Travis CI or Jenkins etc.

Why you’re the best person to speak on this subject?

I am currently a software engineer in the Bloomberg AI group. I have been involved with open source Python development for more than 3 years now. I am also the project lead for MusicBrainz Picard an open-source, cross-platform desktop application with 100k+ users. Among other things, I have written the packaging/distribution code and the CI/CD pipelines for Picard. See Picard announcement blogs for more details. I am also the author of one of the most popular guides about Python Freezing which has amassed 30k+ views.