-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
importlib examples have their exec_module()/sys.modules assignment lines reversed #81702
Comments
unzip the attached zip file and run main.py expected output: True actual output: So what? |
Updated main.py. New expected output: New actual output: False
<module 'testext.sub' (namespace)>
Traceback (most recent call last):
File "main.py", line 14, in <module>
print(module.sub)
AttributeError: module 'testext' has no attribute 'sub' |
Mind re-uploading the examples files with a more appropriate file name? (Saying "import is f-'ed" is not motivating to those of us who wrote it to try and fix this.) |
Updated. Also removed some old code which had irrelevant imports that I thought I removed already. The expected and actual output remain the same. |
Thanks! I'll try to have a look when I can. |
So the issue is the lines assigning to sys.modules and loader.exec_module() are reversed. Had you not done an import in your testext.__init__ you never would have noticed, but since you are then the normal import system is noticing there's nothing in sys.modules and thus inserting a module (importlib.uilt.module_from_spec() always creates a new module so your module that you create never ends up in sys.modules). |
I will have a PR up shortly to fix the docs. |
Thanks for the report, Benjamin! |
Hmm, why is the assignment to sys.modules necessary at all, if module_from_spec() always does so? |
I'm not sure why you think importlib.util.module_from_spec() adds a module to sys.modules? https://docs.python.org/3/library/importlib.html#importlib.util.module_from_spec doesn't say that nor does https://github.com/python/cpython/blob/master/Lib/importlib/_bootstrap.py#L549 actually do that. |
I dunno, one of those three does. I suppose it's spec.loader.exec_module(). |
Nope, loader.exec_module() doesn't either: https://docs.python.org/3/library/importlib.html#importlib.abc.Loader.exec_module, https://github.com/python/cpython/blob/master/Lib/importlib/_bootstrap_external.py#L776. So unfortunately I'm still not sure what you're talking about. |
This prints False, False, True though. |
If you're using your example code for 'testext' that you uploaded then that's expected because the import in your package causes a normal import which means the import machinery is adding things to sys.modules. Try it with a plain module that's empty so you don't have any other imports as a side-effect of imports and you will see the module isn't added to sys.modules. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: