#πŸ”’ How to embed resources in PyInstaller execultable?

11 messages Β· Page 1 of 1 (latest)

south badger
#

I'm trying to "pack" a Python project into an executable with PyInstaller and I can't really seem to find a way to include resources with it
As a start, I need to include a single .ico that's on the root of the project, I do this with --add-data "icon.ico:icon.ico"

To get the resources, the code uses

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

But when running the executable, it just tries to find it alongside the execultable (I guess that the resource_path() doesn't work then)
I've looked up _MEIPASS and it seems like now there's a _PYI_APPLICATION_HOME_DIR instead? I tried changing it, same issue though...
What am I doing wrong?

vague meadowBOT
#

@south badger

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

abstract otter
abstract otter
# south badger

Okay, meipass is there so it's probably how you're including the icon.
--add-data expects strings in the form of source:dest_dir. In this case, it might be literally creating a folder called icon.ico and putting your icon there. So instead, just use a dot and it should work.

south badger
#

A dot how exactly?

abstract otter
south badger
#

Ohh, right. Will try now

#

Yep, that was it! Thanks <3

vague meadowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.