This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pkgutil.get_data() doesn't work with namespace packages
Type: behavior Stage: commit review
Components: Documentation Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: brett.cannon, dgreiman, docs@python, eric.smith, eric.snow, ncoghlan, python-dev, xiang.zhang
Priority: normal Keywords: patch

Created on 2016-10-12 23:36 by dgreiman, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
namespace_package_get_data.sh dgreiman, 2016-10-12 23:36 Unix shell script to demonstrate bug
issue28424.patch dgreiman, 2016-12-10 01:39 review
Messages (8)
msg278543 - (view) Author: Douglas Greiman (dgreiman) * Date: 2016-10-12 23:36
pkg_util.get_data('mypackage', 'resourcename')

returns None if 'mypackage' is a namespace package, because the package object has no __file__ attribute.  A shell script is attached to reproduce this.

This is either a bug to be fixed (my preference) or behavior to be documented.  If there is equivalent, working functionality in the new importlib, that would also be a good to document.
msg278629 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-10-14 04:06
The doc says: "If the package cannot be located or loaded, or it uses a loader which does not support get_data(), then None is returned". Namespace package gets a ``None`` loader and then does not support get_data.
msg278663 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-10-14 20:00
Xiang is right about why this doesn't work. If you would like to propose a patch to update the wording of the docs, Douglas, we could then consider applying it. It could be as simple as just adding "(e.g. the load for namespace packages does not implement get_data())".

And there is no equivalent feature in importlib (yet; it's on my todo list).
msg278699 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2016-10-15 06:55
For 3.4/5/6, I agree this is a documentation issue, where the data files need to be given a *non*-namespaced directory to live in. However, it's worth explicitly noting that subpackages of namespace packages can themselves be regular packages, so one possible solution is to do:

    pkg_util.get_data('my_namespace.my_package_data', 'resourcename')

rather than storing the data directly at the namespace level.
msg282819 - (view) Author: Douglas Greiman (dgreiman) * Date: 2016-12-10 01:39
Patch attached.  Feel free to wordsmith.
msg282870 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-12-10 19:55
Patch LGTM.
msg282880 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-12-10 22:15
New changeset 3484933ba904 by Brett Cannon in branch '3.5':
Issue #28424: Document pkgutil.get_data() doesn't work with namespace packages.
https://hg.python.org/cpython/rev/3484933ba904

New changeset c17d2a37d610 by Brett Cannon in branch '3.6':
Merge for issue #28424
https://hg.python.org/cpython/rev/c17d2a37d610

New changeset b396a2c97871 by Brett Cannon in branch 'default':
Merge for issue #28424
https://hg.python.org/cpython/rev/b396a2c97871
msg282881 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-12-10 22:15
Thanks for the patch, Douglas!
History
Date User Action Args
2022-04-11 14:58:38adminsetgithub: 72610
2016-12-10 22:15:53brett.cannonsetstatus: open -> closed
resolution: fixed
messages: + msg282881
2016-12-10 22:15:31python-devsetnosy: + python-dev
messages: + msg282880
2016-12-10 19:55:40brett.cannonsetversions: + Python 3.7, - Python 3.4
2016-12-10 19:55:07brett.cannonsetmessages: + msg282870
stage: commit review
2016-12-10 01:39:30dgreimansetfiles: + issue28424.patch
keywords: + patch
messages: + msg282819
2016-10-15 06:55:15ncoghlansetmessages: + msg278699
versions: + Python 3.6
2016-10-14 20:00:31brett.cannonsetnosy: + docs@python
messages: + msg278663

assignee: docs@python
components: + Documentation, - Library (Lib)
2016-10-14 04:06:25xiang.zhangsetnosy: + eric.snow, brett.cannon, xiang.zhang, ncoghlan
messages: + msg278629
2016-10-13 19:36:56eric.smithsetnosy: + eric.smith
2016-10-12 23:36:00dgreimancreate