[issue33547] Relative imports do not replace local variables
New submission from Rolf Campbell : Relative imports do not replace local variables, but also don't fail. This can cause some very strange outcomes like this simple example: touch a.py; python3.6 -c 'a=7; b=5; from . import a as b; print(a,b)' I would expect this to produce "7 ", but instead, it produces "7 7". Tested in v3.6.2 and v3.6.5: Python 3.6.5 (default, Mar 29 2018, 18:20:46) [GCC 8.0.1 20180317 (Red Hat 8.0.1-0.19)] on linux -- components: Interpreter Core messages: 316864 nosy: Rolf Campbell priority: normal severity: normal status: open title: Relative imports do not replace local variables type: behavior versions: Python 3.6 ___ Python tracker <https://bugs.python.org/issue33547> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33547] Relative imports do not replace local variables
Rolf Campbell added the comment: Under simple circumstances, this is only reproducible when either directly in an interactive Python session (or as -c), but I encountered this type of problem in a much more complicated project which was NOT running as part of an interactive Python session (or as part of a -c argument). -- ___ Python tracker <https://bugs.python.org/issue33547> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33547] Relative imports do not replace local variables
Rolf Campbell added the comment: Thanks David, I agree that my assumption that the local valiables were not being replaced is not really what was going on there. I also agree that, while this might not strictly classify as a bug, it's probably not the most helpful/useful way that "from ." could be implemented for __main__. Why does it act different than in modules? I have reproduced my original ploblem in a simplified scenario, for which I will raise a new bug. Thanks -- ___ Python tracker <https://bugs.python.org/issue33547> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33547] Relative imports do not replace local variables
Rolf Campbell added the comment: Re-opening because I've found a simple example that does not involve __main__. ./func/__init__.py:func = 1 ./func/__init__.py:from . import func ./func/__init__.py:print(f"Namespace value of func after func module import:{func}") ./func/func.py:print("Module imported") ./main.py:import func ./main.py:def func(x): ./main.py: return x If you create files that look like that, and then run: "python3.6 main.py" I get this output: Namespace value of func after func module import:1 If I comment out the "func = 1" line, then func ends up being imported (and printing as a ). -- status: closed -> open ___ Python tracker <https://bugs.python.org/issue33547> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33547] Relative imports do not replace local variables
Change by Rolf Campbell : -- resolution: not a bug -> ___ Python tracker <https://bugs.python.org/issue33547> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33547] Relative imports do not replace local variables
Rolf Campbell added the comment: OK, OK, I think I finally understand what you mean here. Let me try to repeat it just to make sure I really understand: When requesting a member of a multi-file module (like "func" in my example), python only tries to load that member as a module from disk if there isn't something already created as part of __init__.py. In my case, I'm trying to load "func.func" which I specifically created in line#1 of func/__init__.py, so Python sees no need to even try to load the func/func.py file. If I comment-out the first line of func/__init__.py, then Python fails to find an item called "func.func" and so it tries to load one from disk which causes it to load "func/func.py". My real problem here was that I shouldn't be creating entries in the "func" namespace that clash with on-disk sub-modules that I want loaded. Thanks for your time and effort in explaining this. -- ___ Python tracker <https://bugs.python.org/issue33547> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33547] Relative imports do not replace local variables
Rolf Campbell added the comment: OK, while I understand what you are saying, that is NOT how absolute imports work. I'll give an example: ./main.py:import func ./main.py:print(f"Value of func.func after import func:{func.func}") ./main.py:import func.func ./main.py:print(f"Value of func.func after import func.func:{func.func}") ./func/__init__.py:func = 1 ./func/__init__.py:from . import func ./func/__init__.py:print(f"Value of func after from . import func:{func}") ./func/func.py:print("Module imported") Here, the relative import inside __init__.py does NOT load the "func.py" module because there is already an object called "func". But, the absolute "import func.func" does load "func.py" even though there is already a "func.func" object. Are these supposed to work differently? That seems strange to me. -- ___ Python tracker <https://bugs.python.org/issue33547> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33547] Relative imports do not replace local variables
Rolf Campbell added the comment: Is there any way to use relative imports and explicitly request a sub-module? >From PEP 328: "import <> is always absolute" So it sounds like there is no way to duplicate the explicit request for a sub-module when using relative imports. -- ___ Python tracker <https://bugs.python.org/issue33547> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com