From ... Import' VS 'Import .'

`from ... import` vs `import .`

It depends on how you want to access the import when you refer to it.

from urllib import request
# access request directly.
mine = request()

import urllib.request
# used as urllib.request
mine = urllib.request()

You can also alias things yourself when you import for simplicity or to avoid masking built ins:

from os import open as open_
# lets you use os.open without destroying the
# built in open() which returns file handles.

Use 'import module' or 'from module import'?

The difference between import module and from module import foo is mainly subjective. Pick the one you like best and be consistent in your use of it. Here are some points to help you decide.

import module

  • Pros:
    • Less maintenance of your import statements. Don't need to add any additional imports to start using another item from the module
  • Cons:
    • Typing module.foo in your code can be tedious and redundant (tedium can be minimized by using import module as mo then typing mo.foo)

from module import foo

  • Pros:
    • Less typing to use foo
    • More control over which items of a module can be accessed
  • Cons:
    • To use a new item from the module you have to update your import statement
    • You lose context about foo. For example, it's less clear what ceil() does compared to math.ceil()

Either method is acceptable, but don't use from module import *.

For any reasonable large set of code, if you import * you will likely be cementing it into the module, unable to be removed. This is because it is difficult to determine what items used in the code are coming from 'module', making it easy to get to the point where you think you don't use the import any more but it's extremely difficult to be sure.

“from module import *” VS “import module”

PEP 8 states that

Wildcard imports (from <module> import *) should be avoided, as they
make it unclear which names are present in the namespace, confusing
both readers and many automated tools. There is one defensible use
case for a wildcard import, which is to republish an internal
interface as part of a public API (for example, overwriting a pure
Python implementation of an interface with the definitions from an
optional accelerator module and exactly which definitions will be
overwritten isn't known in advance).

Python : 'import module' vs 'import module as'

The below syntax will help you in understanding the usage of using "as" keyword while importing modules

 import NAMES as RENAME from MODULE searching HOW

Using this helps developer to make use of user specific name for imported modules.
Example:

 import random 
print random.randint(1,100)

Now I would like to introduce user specific module name for random module thus
I can rewrite the above code as

 import random as myrand
print myrand.randint(1,100)

Now coming to your question; Which one is preferred?
The answer is your choice; There will be no performance impact on using "as" as part of importing modules.

imports: `import ...` vs `from ... import ...` when using aliases

import torch.nn as nn

This is a bit confusing since both names end with nn, so with a bit different example-

import torch.nn as x

This is like "assigning" torch.nn in x.

I.e. x.foo is exactly as torch.nn.foo.

from torch import nn

Here nn is an abberviation form. nn.foo is the same as torch.nn.foo.

There is another way to import with named imports:

from torch import nn as x

Which will allow you to call torch.nn module by calling x.

Hope it's more clear now

Difference between import X and from X import * ?

After import x, you can refer to things in x like x.something. After from x import *, you can refer to things in x directly just as something. Because the second form imports the names directly into the local namespace, it creates the potential for conflicts if you import things from many modules. Therefore, the from x import * is discouraged.

You can also do from x import something, which imports just the something into the local namespace, not everything in x. This is better because if you list the names you import, you know exactly what you are importing and it's easier to avoid name conflicts.

Recursive import: 'import' vs. 'from ... import ...'

Cyclic imports usually indicate design problems but in order to solve them you may write the import statement at the bottom like so:

def x1():
print "x1"

def x2():
print "x2"
file2.y2()

from file2 import y2

Keep in mind it's a workaround. The reason from x import y doesn't work in case of cyclic imports is that when you reach the first from ... import ... you are passed to the second module and when the second module calls back the first, the interpreter realizes it's a never-ending cycle and continues with a partially imported module, which happens before you even define the functions meaning y2 doesn't exist yet.



Related Topics



Leave a reply



Submit