[issue33547] Relative imports do not replace local variables

2018-05-25 Thread Nick Coghlan
Nick Coghlan added the comment: Not as a statement, but you can force it with importlib.import_module: $ python3 -c "import pkg; pkg.submodule = 1; import importlib; importlib.import_module('.submodule', 'pkg'); print(pkg.submodule)" pkg pkg.submodule We're getting

[issue33547] Relative imports do not replace local variables

2018-05-24 Thread Rolf Campbell
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

[issue33547] Relative imports do not replace local variables

2018-05-23 Thread Nick Coghlan
Nick Coghlan added the comment: Yes, while weird, that's expected behaviour. Rather than being due to absolute vs relative imports, the difference arises from the fact that in "import pkg.module", the request is explicitly for a submodule, so the submodule import always

[issue33547] Relative imports do not replace local variables

2018-05-23 Thread Rolf Campbell
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

[issue33547] Relative imports do not replace local variables

2018-05-18 Thread R. David Murray
R. David Murray added the comment: Yes, you are substantially correct. A subtlety that may enhance your understanding (if it doesn't instead totally confuse you :) is that __init__ is simply the most straightforward way to affect the module namespace. You would see

[issue33547] Relative imports do not replace local variables

2018-05-18 Thread Rolf Campbell
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

[issue33547] Relative imports do not replace local variables

2018-05-18 Thread R. David Murray
R. David Murray added the comment: It's the same answer. __init__ *is* the package namespace, so you are setting the value of 'func' in the package (.) namespace, and what import is doing is correct. I know this is confusing. I banged my head against it while

[issue33547] Relative imports do not replace local variables

2018-05-18 Thread Rolf Campbell
Change by Rolf Campbell : -- resolution: not a bug -> ___ Python tracker ___

[issue33547] Relative imports do not replace local variables

2018-05-18 Thread Rolf Campbell
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}")

[issue33547] Relative imports do not replace local variables

2018-05-17 Thread Rolf Campbell
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

[issue33547] Relative imports do not replace local variables

2018-05-17 Thread Nick Coghlan
Nick Coghlan added the comment: As David notes, the issue in the example is the fact that you're setting "__main__.a", so "a.py" never gets imported as a module - it gets a hit on the parent module attribute, and hence stops there. -- resolution: -> not a bug

[issue33547] Relative imports do not replace local variables

2018-05-16 Thread R. David Murray
R. David Murray added the comment: It's importing 'a' from '.', which I guess in this context means from the current namespace (__main__), and a is 7. You'll note that 'b' did get repointed, but it got repointed to what 'a' points to, instead of to 5. If it really

[issue33547] Relative imports do not replace local variables

2018-05-16 Thread Rolf Campbell
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

[issue33547] Relative imports do not replace local variables

2018-05-16 Thread Rolf Campbell
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