-
-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using __name__ on a non-package module results in "not a package" error #60
Comments
In GitLab by @jaraco on Jun 3, 2018, 13:53 I note that |
In GitLab by @warsaw on Jun 4, 2018, 21:00 Is there something weird about
Is
Is |
In GitLab by @warsaw on Sep 7, 2018, 19:20 Getting back to this, I guess |
In GitLab by @jaraco on Sep 11, 2018, 09:03 Yes, I suggest that since
(or similar) to resolve the package of a module. Curiously, when I test on Python 2 now, I see that Indeed, just going back to Python 2.7.12, I see
|
In GitLab by @brettcannon on Sep 11, 2018, 18:21 So technically, in Python 3 this is as simple as using As for API change to support this, I'm -0. It's a small nicety, but I would rather people be explicit with what they are anchoring off of rather than calculate it implicitly because someone happened to pass in a submodule. It also alleviates the very minor worry of someone who converted a package to a module and then gets bit by that shift. |
In GitLab by @jaraco on Sep 11, 2018, 22:13 I'm struggling to devise a straightforward way to replace the pkg_resources code for this use-case. So the recommendation is for a module that wishes to load resources from the package containing that module that they write:
That seems somehow less preferable to the code this intends to replace:
Perhaps what you're suggesting is that loading a resource in one's package should not be allowed unless the developer explicitly type the package name. Obviously that's possible, but it does mean that only Python 3 users get the nice experience. If I still don't see why a helper function couldn't provide a compatibility shim: def package_for(globals):
"""
Given a module's globals, return its package.
"""
try:
return globals['__spec__'].parent
except KeyError:
pass
is_module = inspect.ismodule(__import__(globals['__name__']))
return globals['__name__'] if is_module else globals['__name__'].rpartition('.')[0] Or at least provide the Python 2 logic for resolving a package from a module, so one could at least write:
Note, I'm not suggesting changing the API for importlib_resources.read_binary (or its comparable functions), only to provide a means to resolve a package from a module. It seems unnecessarily constraining and brittle to require a module to name its package by name (and then require that to be kept in sync). It seems comparable to only allowing absolute imports and never relative imports. IMO, the resource API should provide for resources relative to a module and not leave it to each module to determine/declare in which package it is contained. If you're not convinced, I'll drop it and focus on the documentation that clarifies this pitfall. |
In GitLab by @warsaw on Sep 12, 2018, 13:02 Why wouldn't it just be
|
In GitLab by @jaraco on Sep 12, 2018, 13:03 See above:
|
In GitLab by @warsaw on Sep 12, 2018, 13:26 WTF? @brettcannon just tried it with a bespoke package layout and you're right, it doesn't work for 2.7.15 in that case. So why does it work for Aside from that, here's the philosophical question. If we add support for this as a nod to the mission of this library to replace So, Brett and I muse that if you were by chance to submit a PR for that and it looked okay, we'd be cool with accepting this one too. But in that case, I think allowing modules (objects and names) in the We'd need to update the documentation too, but I don't think we'd need to change the type signatures. This and #58 would require a major version bump IMO, and I would be disinclined to backport these changes to Python 3.7. |
In GitLab by @jaraco on Oct 31, 2018, 15:02 As I think about this more, I'm leaning toward not proposing a change. I'm particularly reluctant to make the interface(s) less precise, especially since the expanded interface would encourage the anti-pattern (using |
In GitLab by @jaraco on Oct 31, 2018, 15:02 closed |
* Update .coveragerc * Keep whitespace consistent. Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Replace pkg_resources.resource_filename with importlib.resources.as_file. This removes an implicit dependency on setuptools (to which pkg_resources belongs); furthermore, the entire pkg_resources API is deprecated. Regarding the switch from __file__ to __package__, see: python/importlib_resources#60
Replace pkg_resources.resource_filename with importlib.resources.files. This removes an implicit dependency on setuptools (to which pkg_resources belongs); furthermore, the entire pkg_resources API is deprecated. Regarding the switch from __file__ to __package__, see: python/importlib_resources#60
Replace pkg_resources.resource_filename with importlib.resources.files. This removes an implicit dependency on setuptools (to which pkg_resources belongs); furthermore, the entire pkg_resources API is deprecated. Regarding the switch from __file__ to __package__, see: python/importlib_resources#60
In GitLab by @jaraco on May 18, 2018, 11:36
Coming from pkg_resources, I'd previously used
__name__
as the initial parameter:But in importlib_resources, this approach doesn't work.
In other words, importlib_resources seems to be more stringent about the input, requiring the parameter to be a package and not simply a module.
Is this limitation intentional? If so, the migration docs should include a note about this incompatibility.
The text was updated successfully, but these errors were encountered: