[New-bugs-announce] [issue45675] pkgutil.get_data() doesn't add subpackages to namespaces when importing

Matt Wozniski report at bugs.python.org
Fri Oct 29 15:26:00 EDT 2021


New submission from Matt Wozniski <godlygeek at gmail.com>:

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).

----------
components: Library (Lib)
messages: 405331
nosy: godlygeek, pablogsal
priority: normal
severity: normal
status: open
title: pkgutil.get_data() doesn't add subpackages to namespaces when importing
type: behavior
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45675>
_______________________________________


More information about the New-bugs-announce mailing list