Questions about the import ordering

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ChrisWang
    New Member
    • Nov 2008
    • 9

    Questions about the import ordering

    Dear all,

    I am reading the book "Core Python Programming". In the chapter talking about modules, it says the modules should follow this ordering:

    Code:
    import Python Standard Library modules
    
    import Python third party modules
    
    import Application-specific modules
    I am not sure whether the ordering will impact the module's loading or search efficiency. For example, a common module in the standard library can be free from being imported several times, if this module will be imported by application-specific modules.

    test.py
    Code:
    #test.py
    import os
    import MyModuleA
    import MyModuleB
    ...
    MyModuleA.py
    Code:
    #MyModuleA.py
    import os
    ...
    MyModuleB.py
    Code:
    #MyModuleB.py
    import os
    ...
    If we follow the ordering to import the modules, here we import standard library "os", we may save some time, right? It's only my assumption and I am not sure I am right. But I am curious to know whether there are some benefits we can get from the this ordering.

    Could anyone be kind enough to give me the answer? Thanks a lot.

    Have a nice holiday!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    A module is imported and executed only once. A subsequent import of the same module only creates a reference to the module namespace created on the previous import.

    The ordering of the imports is primarily for consistency and is recommended in PEP 8. I don't know if there are any performance benefits.

    -BV

    Comment

    • ChrisWang
      New Member
      • Nov 2008
      • 9

      #3
      Originally posted by bvdet
      A module is imported and executed only once. A subsequent import of the same module only creates a reference to the module namespace created on the previous import.

      The ordering of the imports is primarily for consistency and is recommended in PEP 8. I don't know if there are any performance benefits.

      -BV
      Thanks BV. Could you please show me detailed explanation about the consistency?
      I've read the description about imports in PEP08, but I think I am still puzzled about that.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        ChrisWang,

        Sorry, I have no detailed explanation. I think the reference to consistency is referring to Python applications in general.

        If a top level module imports a required Python library module and another application specific module is imported and executed and it imports the same Python library module, a reference is made to the module instead of it being importing again. If you think about it, it makes sense to order the imports that way.

        I would not import a module unless the current module requires it.

        -BV

        Comment

        • ChrisWang
          New Member
          • Nov 2008
          • 9

          #5
          Ok. Thank you very much.

          Have a great holiday!

          Comment

          Working...