On Sat., Jun. 5, 2021, 07:38 Robin Becker,
On 04/06/2021 19:29, Steve Dower wrote: ...........
While we were investigating the reliably broken behaviour, we figured
that related behaviour was
*unreliably* broken on all platforms.
Specifically, if you imported a module through a relative path (resolved to CWD), changed the CWD, cleared the module cache, and reimported, you would still get the original module, contrary to the intent of getting the newly resolved module. ("Correct" code would have also cleared the import path caches, not just sys.modules.) This was because the module info was cached using the relative path, and so would be used later even though its absolute path had changed.
Someone reported this change in 3.8 and we decided to revert it in existing released, because the bugfix was for an obscure enough situation that it really wasn't worth breaking any existing versions. For unreleased versions, it's better to fix it so that imports will be more reliable in the future.
Unfortunately we are using these relative imports to implement plugins for an xml language processor so this is likely not just an accident of ordering the failing tests. I agree we have to get a plugin module a bit more directly using the import internals. I still think we might need to control sys.path as presumably the plugin code could have imports of parallel modules.
I'm beginning to understand that messing about with sys.path/modules/meta_path etc etc is a lot harder than it used to be.
How so? The key details have not changed since Python 3.4 when module specs were introduced. And meta path, etc. are 2.7 concepts.
Incidentally I find that I am able to import a namespace module directly; this code doesn't crash (at least in python >= 3.6
################################################### #timpA.py don't run with any meaningful directory A import importlib def main(): module = importlib.import_module('A') print('module=%r' % module)
if __name__=='__main__': main() ###################################################
in python 3.6-3.9 this prints out "module=
" in 3.10.0b1 it prints "module= )>". I thought I had to use pkgutils or pkg_resources to mess with these new-fangled packages.
Nope, that has never been required since the concept of namespace packages were introduced to import.
Likewise I find that the python statement 'import A' also doesn't fail and A is a namespace.
Yep, that's expected. I feel quite stupid now :(
As soon as you step outside the norm there are a lot of details to understand; import is very much following the "Complex is better than complicated" zen. So no need to feel stupid, there's a lot to keep track of when you start to dive into the details. -Brett --
Robin Becker _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/KMJAQUGS... Code of Conduct: http://python.org/psf/codeofconduct/