Editor

Find Bundled Files

Intermediate · Write the code · from tkinter to .exe with PyInstaller: Windows, macOS and Linux

From the lesson: tkinter to .exe with PyInstaller: Windows, macOS and Linux

Turn your tkinter program into an .exe, a Mac app or a Linux program that people can open with a double-click, with no Python on their computer.

Read the lesson

Write resource_path(relative) that returns the full path of a file your app ships with:

  • In a PyInstaller bundle, sys has a _MEIPASS attribute: join relative onto that folder.
  • Otherwise, join it onto the folder of the script, os.path.dirname(os.path.abspath(__file__)).
  • # in a bundle unpacked to /tmp/_MEI52162
    resource_path("logo.png")  ->  /tmp/_MEI52162/logo.png
    
    # as the script /home/ana/tip-calculator/app.py
    resource_path("logo.png")  ->  /home/ana/tip-calculator/logo.png
    import os
    import sys
    def resource_path(relative):
    # In a bundle: start from sys._MEIPASS
    # Otherwise: start from the folder of __file__
    pass

    What Check runs

    Your code runs first, then each of these lines, and 3 more that stay hidden. The output must match exactly.

    RunsExpected output
    import sys sys.frozen, sys._MEIPASS = True, "/tmp/_MEI52162" try: print(resource_path("logo.png")) finally: del sys.frozen, sys._MEIPASS/tmp/_MEI52162/logo.png
    import sys vars(sys).pop("frozen", None) vars(sys).pop("_MEIPASS", None) __file__ = "/home/ana/tip-calculator/app.py" print(resource_path("logo.png"))/home/ana/tip-calculator/logo.png

    More PyInstaller exercises

    All Python exercises · 50 practice problems