How to use PyInstaller to create Python executables

How to use PyInstaller to create Python executables

  • Build your PyInstaller package on the deployment operating system: PyInstaller does not support cross-platform builds. If you need to deploy your standalone Python application on MacOS, Linux, and Windows systems, then you will need to install PyInstaller and build separate versions of the application on each of these operating systems. 
  • Build your PyInstaller package as you develop your app: As soon as you know you will be deploying your project with PyInstaller, build your .spec file and start refining the PyInstaller package in parallel with the development of your application. This way, you can add exclusions or inclusions as you go, and test the way new features are deployed with the application as you write them.
  • For apps with a module entry point, make a stub: Some applications use a module as an entry point, for instance by having a __main__.py file that’s invoked when the module is imported. PyInstaller doesn’t have a mechanism for using such a module as an entry point. To make that work, create a “stub” file—a .py file in the top levet of your project that takes the same steps to run your application as the  contents of the __main__.py file. Then feed PyInstaller the stub to perform the analysis.
  • Don’t use PyInstaller’s –onefile mode: PyInstaller includes a command line switch, --onefile, that packs your entire application into a single self-extracting executable. This sounds like a great idea—you only have to deliver one file!—but it has pitfalls. On WIndows, whenever you run the application, it must first unpack all of the files within the executable to a temporary directory. If the application is big (200MB, for instance), unpacking can mean a delay of several seconds. To get around this, use the default single-directory mode instead, and just pack everything up as a .zip file.
  • Create an installer for your PyInstaller app: If you want a way to deploy your application other than a .zip file or the single-file distribution, consider using an installer utility like the open source Nullsoft Scriptable Install System. It adds very little overhead to the size of the deliverable and lets you configure many aspects of the installation process, like creating shortcuts to your executable.
  • Use code signing on WIndows to mark the generated executables: If you have a code-signing certificate and want to use it to sign a PyInstaller-generated project to keep it from being flagged as malware, there’s a recipe for doing this. To save time, you can integrate the signing process into the project’s .spec file.
  • Don’t expect speedups: PyInstaller is a packaging system, not a compiler or an optimizer. Code packaged with PyInstaller does not run any faster than it would when run on the original system. If you want to speed up Python code, use a C-accelerated library suited to the task, or a project like Cython.

Add a Comment