Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Glenn Ramsey: Packaging a Python desktop application using PyInstaller

Glenn Ramsey: Packaging a Python desktop application using PyInstaller

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Glenn Ramsey:
Packaging a Python desktop application using PyInstaller
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
@ Kiwi PyCon 2014 - Saturday, 13 Sep 2014 - Track 2
http://kiwi.pycon.org/

**Audience level**

Intermediate

**Description**

What PyInstaller does. How it works. How to use it. What to do when it doesn't work.

**Abstract**

This talk will be an introduction to using PyInstaller for packaging a desktop application on Windows and OSX. It will briefly outline how PyInstaller works and describe how to use it, with some simple examples. Finally some techniques to use for debugging a built package will be discussed.

**YouTube**

https://www.youtube.com/watch?v=tOTLqUQC-k0

New Zealand Python User Group

September 13, 2014
Tweet

More Decks by New Zealand Python User Group

Other Decks in Programming

Transcript

  1. What it does Makes the application independent Gathers python files

    and dependencies Embeds python interpreter and libraries Source is not included Does not actually create an installer!
  2. Where it can be used Windows + OS X +

    Linux + … Licensing PyInstaller itself – GPL >=2 Packaged program – Your choice Python 2.4 – 2.7 ( 3 - real soon now) Well documented
  3. Alternatives Py2exe - Windows Py2app - OS X BbFreeze -

    Win + OS X + Linux CxFreeze – Win + OS X + Linux (Python 3)
  4. How it works When launched a bootloader is started first.

    Bootloader Creates Python environment Starts Python interpreter Runs your program
  5. How to use it onefile and onedir (default) modes python

    -o pyinstaller.py /path/to/main.py On first run it will create a spec file, then use: python -o pyinstaller.py /path/to/main.spec
  6. A .spec file – analysis a = Analysis(myscript.py,...) Analyses imports

    Produces: a.scripts the python scripts named on the command line a.pure pure python modules needed by the scripts a.binaries non-python modules needed by the scripts
  7. A .spec file – pyz and exe pyz = PYZ(a.pure)

    Creates a compressed archive containing pure python modules exe = EXE(pyz, a.scripts + options, ...) Creates an executable with the embedded python interpreter
  8. A .spec file – Collect and Bundle coll = Collect(exe,

    a.binaries, a.zipfiles, a.datas, ...) Collects all files into a single directory app = Bundle(coll, name='MyApp' icon='/path/to/icon') OSX only – creates the app bundle
  9. Including files Create a TOC – a tuple will suffice

    extra = [("defaults.cfg", os.path.join('..', 'GUI', 'defaults.cfg'), "DATA")] coll = collect(..., a.datas + extra) Can't include files outside the app dir Problem for OS X therefore use the build system
  10. Excluding files a.binaries - [('badmodule', None, None)] a.binaries = [x

    for x in a.binaries if \ not x[0].startswith("scipy")] a.binaries = [x for x in a.binaries if \ Not os.path.dirname(x[1]).startswith( "C:\\Windows\\system32")]
  11. Accessing files from a frozen app import sys import os

    ... if getattr(sys, 'frozen', False): # we are running in a |PyInstaller| bundle basedir = sys._MEIPASS if is_osx: res_path = os.path.join( sys._MEIPASS, "..", "Resources") else: # we are running in a normal Python env basedir = os.path.dirname(__file__)
  12. OSX specific files outside the bin dir e.g. plugins -

    use Cmake - use install_name_tool Info.plist LSBackgroundOnly
  13. Hooks Example: countrycode module Collects the file countrycode_data.csv from site-packages

    from PyInstaller.hooks.hookutils import collect_data_files datas = collect_data_files('countrycode')
  14. Hidden Imports For imports that PyInstaller can't find e.g. inside

    a function definition Can be used to workaround broken hooks Or just to force an import
  15. Setting an icon Pass the icon path as a parameter

    to: the EXE class - Windows The Bundle class – OSX ..., icon = '/path/to/icon', ...
  16. Actually creating an installer OSX – Drag'n'Drop or PackageMaker Windows

    - NSIS installer Linux - deb, rpm, bin Cmake / CPack
  17. Using with a build system Cmake Use template to create

    main.spec with correct paths Create rules to run PyInstaller
  18. Debugging 1. Check the log 2. -d – bootloader writes

    messages to stdout In spec file: Exe = EXE(..., debug = True, console = True) 3. If that doesn't work, then panic and get out the debugger. Live example - Simple PyQt5 application (wish me luck!) https://github.com/glennra/kpc2014