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.

Author godlygeek
Recipients godlygeek, pablogsal
Date 2021-10-29.19:26:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1635535560.24.0.0921518602941.issue45675@roundup.psfhosted.org>
In-reply-to
Content
If a module hasn't yet been imported, `pkgutil.get_data(pkg_name, data_file)` will import it, but when it does, it doesn't add the submodule to its parent package when the parent package is a PEP 420 implicit namespace package.

```
$ mkdir -p namespace/package
$ touch namespace/package/__init__.py
$ echo data >namespace/package/data_file
$ python3.10 -c 'import namespace.package, pkgutil; print(pkgutil.get_data("namespace.package", "data_file")); import namespace; print(namespace.package)'
b'data\n'
<module 'namespace.package' from '/tmp/namespace/package/__init__.py'>
$ python3.10 -c 'import pkgutil; print(pkgutil.get_data("namespace.package", "data_file")); import namespace.package; import namespace; print(namespace.package)'
b'data\n'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: module 'namespace' has no attribute 'package'
$
```

In this reproducer, we've got an implicit namespace package called "namespace" and a regular package inside it called "namespace.package". The regular package has a data file called "data_file".

If we import the regular package and then call pkgutil.get_data() to access the data file, it successfully retrieves the data file, and the module object for the namespace package winds up with an attribute referencing the module object for the regular package.

If we call pkgutil.get_data() to access the data file before importing the regular package, it successfully retrieves the data file, but the module object for the namespace package doesn't have an attribute referencing the module object for the regular package, even if we later do a normal import for the regular package.

It looks like pkgutil is importing the module when it hasn't already been imported (which I would expect) and adding it and any parent packages to sys.modules (which I would also expect), but then not adding submodules as attributes to their parent modules like `import` would (which seems like a bug).
History
Date User Action Args
2021-10-29 19:26:00godlygeeksetrecipients: + godlygeek, pablogsal
2021-10-29 19:26:00godlygeeksetmessageid: <1635535560.24.0.0921518602941.issue45675@roundup.psfhosted.org>
2021-10-29 19:26:00godlygeeklinkissue45675 messages
2021-10-29 19:26:00godlygeekcreate