5

Given that foo.bar is a module, what is the difference between

import foo.bar as bar

and

from foo import bar

I'm particularly interested in what lazy importing of modules does to this.

Note: This is not a duplicate of this question.

John Oxley
  • 14,698
  • 18
  • 53
  • 78
  • For whoever finds this question, check out [this](https://stackoverflow.com/a/61472248/5976530) answer. – EhsanK Mar 15 '21 at 15:40

1 Answers1

0

In the first code line:

 import foo.bar as bar

Here you are importing bar from foo package but why added as bar means when you need to access any function func in that bar. You have to access like foo.bar.func but when you added as bar you just use

 bar.func

The same to the line from foo import bar just importing the bar

  • I don't think that's true. Trying both, and using `dir()` and `dir(bar)` to check, the namespaces look identical. Functions within `bar` are available in both. – Simon Fraser May 20 '18 at 09:22
  • @SimonFraser: My explanation mean the accessibility of functions in a module –  May 20 '18 at 09:24
  • In both examples a function `func1` in bar is accessible with `bar.func1()` – Simon Fraser May 20 '18 at 09:26
  • As @SimonFraser says, these are completely equivalent when you're using them. I want to know if there is any difference to what Python does with loading imports – John Oxley May 24 '18 at 11:56