0

What is the different between import library as lib and from library import (....)? I saw people used from library import (....) and import library as lib. And wonder which one is the best practice.

Can you help me? Thank you.

curiouscheese
  • 117
  • 1
  • 9
  • 1
    People do ```import lib as l``` too. Example: ```import tkinter as tk```, ```import matplotlib.pyplot as plt```. ```from library import (....)``` can also have a wild card import or putting an asterick: ```from tkinter import *```. But this is general discouraged because the classes and methods can clash. ```import library``` is just importing the library. It doesn't matter to python. Only the user will have to type the entire library to reference the classes –  Aug 13 '21 at 03:59
  • My question is why some user use `from library import (....)` compared the one that you said? Any advantage in using `from library import (....)` compared to `import library as lib`? – curiouscheese Aug 13 '21 at 04:02
  • 1
    When you do ```from library import ...```, you just import those classes. If you reference something that is present in the library but not imported, it will raise an error –  Aug 13 '21 at 04:05
  • 1
    It's mostly personal preference. If I use `from library import xxx`, then someone reading my code later on doesn't immediately know where `xxx` came from. If I use `import library`, then I need to write `library.xxx`, and the source is immediately obvious. Thus, I tend not to use `from library import xxx` very much. – Tim Roberts Aug 13 '21 at 04:11
  • 1
    Does this answer your question? [Use 'import module' or 'from module import'?](https://stackoverflow.com/questions/710551/use-import-module-or-from-module-import) – user202729 Aug 13 '21 at 05:12
  • 1
    Questions about what one *should* do are generally off topic; please see [ask] and https://stackoverflow.com/help/on-topic. Please note that Stack Overflow is *not a discussion forum*; if you want to solicit opinions, your question is a better fit for Reddit or Quora. – Karl Knechtel Aug 13 '21 at 05:16
  • @KarlKnechtel i think this question is good enough as I am a new user of python that confuse what is the best practice to import library into the code. – curiouscheese Aug 13 '21 at 12:28
  • @KarlKnechtel already edited. thank you for the advice. – curiouscheese Aug 14 '21 at 06:12
  • @KarlKnechtel do you have any suggestion on how should the question be edited? I would like to follow it. – curiouscheese Aug 15 '21 at 06:44
  • @KarlKnechtel but, someone has answered this question below which has a really good answer. I don't see any problem here. – curiouscheese Aug 16 '21 at 02:04

1 Answers1

2

There is no functional difference between the two, but for aesthetics, readability and maintainability reasons there are subtle pros and cons to both.

Some common considerations:

  • If a name in the imported module is referenced a lot, it may appear verbose to have the name prepended with its module name every time it is used, e.g. having to repeatedly write lib.func instead of just func. In this case it would make the code look cleaner to import the name from the module so that the name can be used without the module name. For example, if you have a complicated formula such as y = sqrt(cos(x)) - sqrt(sin(x)), you don't want to make the code look more complicated than it already is with y = math.sqrt(math.cos(x)) - math.sqrt(math.sin(x)).
  • If there are a large number of names one wishes to use from an imported module, it would appear too verbose to exhaustively list all those names with a from lib import a, b, c, d... statement. For example, it is common to just import ast for syntax tree traversal since many cases of ast involve references to well over 10 node types, so a simple import ast is usually preferred over an excessively long statement of from ast import Module, Expression, FunctionDef, Call, Assign, Subscript, ....
  • The long statement above also showcases its maintainability issue. If, over time, the logics of the code involves more node types, one would have to add the newly referenced node types to the long list of names imported from the module. Conversely, if one of the names imported from the module becomes unused over time, one should remove it from that long list. None of these would be an issue when you use import ast.
  • Importing names from a module pollutes the namespace of the current module, increasing the likelihood of collision with local names or names imported from other modules. This is especially likely when the language of the imported name is a generic one. For example, it is discouraged to do from re import search because search is such a commonly used name, and there may very well be a local variable named search or a function named search imported from another module to cause name collisions.
  • An additional point to the example above is that writing search(...) makes your code less readable than writing re.search(...) also because search is too generic a term. A call to re.search makes it clear that you're performing a regex search while a call to search looks ambiguous. So use from lib import a only when a itself is a specific term, such as from math import sqrt.
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • 1
    This answer is really good! I love how you answer this. I dont know why StackOverflow want me to edit the queston, but thank you a lot! – curiouscheese Aug 13 '21 at 13:03