Skip to content

Replace __file__ with newer APIs #674

@jayvdb

Description

@jayvdb

#533 replaced custom data lookup with __file__, which works only if the resources are exported to a filesystem. See indygreg/PyOxidizer#69 for more info about why, but the tl;dr version is __file__ is an optional attribute and should not be relied upon. It also requires each zip/pack tool to fiddle with __file__.

Here is the current backtrace when __file__ is not provided:

  File "babel", line 20, in <module>
    from babel.core import UnknownLocaleError, Locale, default_locale, \
  File "babel.core", line 14, in <module>
    from babel import localedata
  File "babel.localedata", line 24, in <module>
    _dirname = os.path.join(os.path.dirname(__file__), 'locale-data')
NameError: name '__file__' is not defined

There are stdlib APIs for this type of access, especially the now ancient pkgutil.get_data, and the newer importlib.resources and backport importlib_resources.

Loading of resources from the runtime package should ideally be done using importlib.resources and its backport , however pkgutil could be used to avoid the need for the backport. Using pkgutil doesn't work under PyOxidizer 0.4, however I expect that will be fixed soon.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions