what is the exact way to import in python and which is the best practise to import file there is lot of way to import so

  1. you guys can import like this
from . import *
  1. or like this
from .models import *
  1. or like this
from .models import Student
  1. or like this
from . import views

2 answers

  • answered 2018-04-17 04:57 ZaxR

    From PEP8: 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).

    Generally you want to import only what you need, and make sure imports are explicit (e.g. the third option in your list, but sometimes also the fourth option).

  • answered 2018-04-17 04:57 fhorrobin

    In general it depends on what you are trying to do and what your style of programming is. Though they all may work, I would suggest sticking with the more explicit styles such what you have listed as (3) and (4).

    The problem with using the * to import everything at once is that they are prone to namespace errors and it is unclear what modules you are actually using.

    Though it seems like more work it will lead to better programs that are easier to follow if you stick with importing only what you need as you need it.