New submission from Matt Wozniski <[email protected]>:
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 <[email protected]>
<https://bugs.python.org/issue45675>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com