GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Unable to load resources for modules · Issue #203 · python/importlib_resources

Unable to load resources for modules #203

New issue
Copy link
New issue
Copy link
Closed
#258
Closed
Unable to load resources for modules#203
#258
Copy link
Assignees
jaraco

Description

@jaraco
jaraco
opened on Nov 7, 2020
Issue body actions

In #60, I encountered an issue where importlib resources was unable to supply an interface for modules that wish to load neighboring resources. I've encountered another use-case where it's not straightforward to replace the pkg_resources usage with importlib.resources.

Again, in this case, because the test module isn't in a package, importlib resources doesn't support it, but pkg_resources does. The current workarounds include:

  • make the tests directory a proper package. However, doing this also causes that package to get discovered as a package.
  • assume the files are on a local file system and access __file__ to infer the location of nearby resources.

In my opinion, if 'importlib' supports importing non-package modules, 'importlib resources' should support loading resources pertinent to those modules.

Activity

  1. FFY00 commented on May 13, 2021

    @FFY00
    FFY00
    on May 13, 2021
    Member
    More actions

    Shouldn't os.path.abspath(os.path.join(__file__, '..', 'empty.docx')) work there? This is the most used solution, I have done this myself numerous times.

    Another option would be __loader__.get_data('empty.docx'), which would work most times.

  2. FFY00 commented on Oct 22, 2021

    @FFY00
    FFY00
    on Oct 22, 2021
    Member
    More actions

    Would it be possible to make files accept a ModuleSpec? Making it possible to do files(__spec__) to get the resources for the current module.

    Currently, the best option to plug the current module into to files IMO is files(sys.modules[__name__]).

  3. FFY00 commented on Oct 23, 2021

    @FFY00
    FFY00
    on Oct 23, 2021
    Member
    More actions

    @jaraco I would like to hear your feedback on this proposal when you have time. Personally, I have mixed feelings, but can't come up with anything better.

    Actually, I would really like for Python to add a __module__ global, but that is likely not gonna happen. I will have to keep using sys.modules[__name__] 🙃

  4. jaraco commented on Jul 22, 2022

    @jaraco
    jaraco
    on Jul 22, 2022
    MemberAuthor
    More actions

    Today I noticed the comments and they were minimized, though I'm unsure why. I've unhidden the comments as they seem valid and in good faith.

  5. jaraco commented on Jul 22, 2022

    @jaraco
    jaraco
    on Jul 22, 2022
    MemberAuthor
    More actions

    Shouldn't os.path.abspath(os.path.join(__file__, '..', 'empty.docx')) work there? This is the most used solution, I have done this myself numerous times.

    Sure, if you wish to assume that all resources are on the file system and if you're willing to force the user to encode the structure of their modules into their implementation, then that's fine. However, it's these use-cases that importlib resources aims to abstract away and that pkg_resources already provides a simple, elegant solution.

    Another option would be __loader__.get_data('empty.docx'), which would work most times.

    Yes, but again, I'd rather provide a uniform solution for users instead of a set of instructions that boils down to: "use importlib resources when you have resources in packages, but use some other technique when you have resources alongside modules."

    Would it be possible to make files accept a ModuleSpec? Making it possible to do files(__spec__) to get the resources for the current module.

    Yes, I was thinking something like that. Especially if files(__spec__) provides a consistent experience whether the module is top-level or a module in a package or is itself a package.

  6. FFY00 commented on Jul 22, 2022

    @FFY00
    FFY00
    on Jul 22, 2022
    Member
    More actions

    Yeah, I marked them as "outdated" as I looked into the issue and realized the proposals did not make much sense.

    Reacted by Jason R. Coombs
  7. jaraco
    self-assigned this
    on Jul 22, 2022
  8. jaraco
    added 4 commits that reference this issue on Jul 22, 2022

    Add test capturing current behavior and missed expectation. Ref #203.

    190f2b5

    Add test capturing current behavior and missed expectation. Ref #203.

    f3364a9

    Remove restriction that a 'package' cannot be a module. Allows resolu…

    ecffc07

    Update changelog. Ref #203.

    421db9c
  9. jaraco
    added 3 commits that reference this issue on Oct 5, 2022

    Add test capturing current behavior and missed expectation. Ref #203.

    94d59b6

    Remove restriction that a 'package' cannot be a module. Allows resolu…

    5b1281d

    Update changelog. Ref #203.

    a60496b
  10. jaraco
    closed this as completedin #258on Oct 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @jaraco
    jaraco

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.