python has two basic import syntax, they are
import module
from module import x
what is the difference and when do I use them?
from module import x Link to heading
the attributes and methods of the imported module types are imported directly into the local namespace, so they are available directly, without qualification by module name.
for example:
when I use import types, I want to call function FunctionType in types module, I have to do this
>>> types.FunctionType()
vs.
when I use from types import FunctionType, I can do this
>>> FunctionType()
when to use which Link to heading
from module import- If you will be accessing attributes and methods often and don’t want to type the module name over and over, use from module import.
- If you want to selectively import some attributes and methods but not others, use from module import.
import module- If the module contains attributes or functions with the same name as ones in your module, you must use import module to avoid name conflicts.