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` causes future imports of children modules to fail.
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry, brett.cannon, cnezin, p-ganssle
Priority: normal Keywords:

Created on 2020-08-10 20:37 by cnezin, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
pkgutil_err.tar cnezin, 2020-08-10 20:37 Package to reproduce errors
Messages (3)
msg375149 - (view) Author: Cory Nezin (cnezin) Date: 2020-08-10 20:37
As demonstrated in this Stack Overflow question (https://stackoverflow.com/questions/59377661/python-pkgutil-get-data-disrupts-future-imports) using `pkgutil.get_data` on a module causes future imports of children modules to fail.  I tracked this down to a call to `importlib._bootstrap.load` here: https://github.com/python/cpython/blob/3.8/Lib/pkgutil.py#L627

This seems to do something to sys.modules which I don't quite understand but I think it is caching the module when it shouldn't be.  If I replace the linked line with `importlib.import_module(package))` it seems to work okay.
msg377679 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-09-29 18:49
I wouldn't use pkgutil.get_data() -- or pkgutil, period -- and instead use importlib.resources to read data files from a package (which is available as a third-party package on PyPI if you need it for older versions of Python).
msg377682 - (view) Author: Cory Nezin (cnezin) Date: 2020-09-29 20:56
Wow, I did not know about that (probably because I've been stuck with Python3.6 for a while). Thank you!
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85691
2020-09-29 20:56:54cnezinsetmessages: + msg377682
2020-09-29 18:49:06brett.cannonsetmessages: + msg377679
2020-09-29 14:38:34pablogsalsetnosy: + barry, brett.cannon, p-ganssle
2020-08-10 20:37:28cnezincreate