14

In situations where you want to import a nested module into your namespace, I've always written it like this:

from concurrent import futures

However, I recently realized that this can be expressed using the "as" syntax as well. See the following:

import concurrent.futures as futures

Which has the subjective advantage of looking more similar to other imports:

import sys
import os
import concurrent.futures as futures

... with the disadvantage of added verbosity.

Is there a functional difference between the two, or is one officially preferred in a PEP or otherwise?

Velovix
  • 527
  • 1
  • 6
  • 19
  • 4
    Possible duplicate of ['import module' or 'from module import'](https://stackoverflow.com/questions/710551/import-module-or-from-module-import) – user3483203 May 11 '18 at 19:39
  • @chrisz: That covers the difference between `import module` and `from module import thing`; it doesn't cover `import module.thing as thing`. – user2357112 May 11 '18 at 19:41
  • 6
    Note that `import foo.bar as bar` only works if `foo.bar` is itself a module, while `from foo import bar` works for *any* module-level name in `foo`. – chepner May 11 '18 at 19:41
  • 1
    the `as` was created to avoid the collision between modules/libs/funcs with same name, an alternative namespace.... – kip May 11 '18 at 19:41
  • duplicate of https://stackoverflow.com/questions/710551/use-import-module-or-from-module-import and I got the question in a review audit :( – anhoppe Jun 08 '18 at 20:25

2 Answers2

7

There are a few functional differences. First, as already mentioned in the comments, import package.thing as thing requires thing to be a module (or a subpackage, which is not actually a separate case because packages count as modules).

Second, in Python 3.5 and later, if from package import thing finds that the module object for package does not have a thing attribute, it will try to look up sys.modules['package.thing'] as a fallback. This was added to handle certain cases of circular relative imports. import package.thing as thing does not yet perform this handling, but it will in Python 3.7.

user2357112
  • 260,549
  • 28
  • 431
  • 505
-1
import concurrent.futures as futures

Allows you to reference the module under the futures namespace in your code (as opposed to concurrent.futures without the as syntax). However, the typing is mostly redundant--you're importing something and declaring its name to be the exact same thing. The standard syntax for this type of import is the from <package> import <module>.

The salient point about your question is that the as syntax is mainly to support multiple imports of the same name without clobbering each other. For example.

from concurrent import futures
from other.module import futures as my_futures

Anything else and you are abusing the as syntax and to me would be considered anti-pattern, because the language provided you a correct way to do what you wanted.

James
  • 2,488
  • 2
  • 28
  • 45
  • 1
    The `as` is not strictly redundant in that example because without the `as`, `concurrent` is introduced in the namespace, not `futures`. – Velovix May 11 '18 at 19:43
  • 4
    Not true. When you `import concurrent.futures` you need to address it as `conncurrent.futures`. When you `import concurrent.futures as futures` you address it as `futures`. – tdelaney May 11 '18 at 19:43