0

Hello dear fellow SO community I was wondering what is the difference between the following two statements:

from os import path  #importing symbol
import os.path as path #importing module as symbol?

since this question was tagged duplicate for this topic I would like to extend the question.

In the above "duplicate" link, 2nd answer (by Michael Ray Lovett) shows that, importing the module affects the module's variables globally while importing the symbol only, affects the local implementation

I would like to know;

  1. Does both of them import symbols only (locally affected) or does module importing (2nd option) let the global change of the variable

  2. Does importing module take less space in ram since importing symbol initiates unique variables per import (which takes ram space)

Ulsa
  • 11
  • 3
  • 1
    There is no difference. – MattDMo Aug 18 '21 at 11:33
  • So from os import path is implemented as import os.path as path in background ? – Ulsa Aug 18 '21 at 11:35
  • @Ulsa as MattDMo said there is usually no difference. I would prefer to do from import os import path if you only needed the path. If you imported it like so: import os.path as path then it would import everything within the os.path module – UrDistraction Aug 18 '21 at 11:36
  • 1
    @UrDistraction you're confusing the issue. `os.path` *is* a module. The only difference in the statements above is personal preference. See the linked dupe. – MattDMo Aug 18 '21 at 11:39
  • 1
    @Ulsa I'm not sure of the implementation details behind the scenes as to which one is translated to the other one, or if they're both translated to something new entirely, but the end result of both is that you now have `path` in your namespace and can use all the functions and classes associated with it. – MattDMo Aug 18 '21 at 11:42
  • @MattDMo I have updated the question. Could you please re-elaborate? – Ulsa Aug 18 '21 at 11:46
  • @Ulsa according to [documentation](https://docs.python.org/3/library/functions.html#__import__) there is no difference between these two import calls, as per the docs `import pckg.module` will return the `pckg` so it will be equivalent of `import pckg`, but the `as` keyword simply allows using the given name that accesses `pckg.module`, but still imports `pckg` (but no direct acces to it). Using `from` would only return the parent package but in this case `from os import path`, `os` is the parent package so it will be imported, but will allow the use only of `path`, so literally no difference – Matiiss Aug 18 '21 at 12:07
  • @Ulsa there should be difference if you did this: `import pckg.parent.module as module` vs `from pckg.parent import module` since the former would return all of the `pckg` but allow using only `pckg.parent.module` but the latter will return only `parent` so allow using only `parent.module` (under the alias). Or so I understand at least, simplest (not really) is if you [read the docs](https://docs.python.org/3/library/functions.html#__import__) – Matiiss Aug 18 '21 at 12:15

0 Answers0