Should import statements always be at the top of a module?

后端 未结 20 1712
醉酒成梦
醉酒成梦 2020-11-22 02:56

PEP 08 states:

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.<

相关标签:
20条回答
  • 2020-11-22 03:33

    Most of the time this would be useful for clarity and sensible to do but it's not always the case. Below are a couple of examples of circumstances where module imports might live elsewhere.

    Firstly, you could have a module with a unit test of the form:

    if __name__ == '__main__':
        import foo
        aa = foo.xyz()         # initiate something for the test
    

    Secondly, you might have a requirement to conditionally import some different module at runtime.

    if [condition]:
        import foo as plugin_api
    else:
        import bar as plugin_api
    xx = plugin_api.Plugin()
    [...]
    

    There are probably other situations where you might place imports in other parts in the code.

    0 讨论(0)
  • 2020-11-22 03:43

    It's a tradeoff, that only the programmer can decide to make.

    Case 1 saves some memory and startup time by not importing the datetime module (and doing whatever initialization it might require) until needed. Note that doing the import 'only when called' also means doing it 'every time when called', so each call after the first one is still incurring the additional overhead of doing the import.

    Case 2 save some execution time and latency by importing datetime beforehand so that not_often_called() will return more quickly when it is called, and also by not incurring the overhead of an import on every call.

    Besides efficiency, it's easier to see module dependencies up front if the import statements are ... up front. Hiding them down in the code can make it more difficult to easily find what modules something depends on.

    Personally I generally follow the PEP except for things like unit tests and such that I don't want always loaded because I know they aren't going to be used except for test code.

    0 讨论(0)
  • 2020-11-22 03:43

    Module initialization only occurs once - on the first import. If the module in question is from the standard library, then you will likely import it from other modules in your program as well. For a module as prevalent as datetime, it is also likely a dependency for a slew of other standard libraries. The import statement would cost very little then since the module intialization would have happened already. All it is doing at this point is binding the existing module object to the local scope.

    Couple that information with the argument for readability and I would say that it is best to have the import statement at module scope.

    0 讨论(0)
  • 2020-11-22 03:43

    Readability

    In addition to startup performance, there is a readability argument to be made for localizing import statements. For example take python line numbers 1283 through 1296 in my current first python project:

    listdata.append(['tk font version', font_version])
    listdata.append(['Gtk version', str(Gtk.get_major_version())+"."+
                     str(Gtk.get_minor_version())+"."+
                     str(Gtk.get_micro_version())])
    
    import xml.etree.ElementTree as ET
    
    xmltree = ET.parse('/usr/share/gnome/gnome-version.xml')
    xmlroot = xmltree.getroot()
    result = []
    for child in xmlroot:
        result.append(child.text)
    listdata.append(['Gnome version', result[0]+"."+result[1]+"."+
                     result[2]+" "+result[3]])
    

    If the import statement was at the top of file I would have to scroll up a long way, or press Home, to find out what ET was. Then I would have to navigate back to line 1283 to continue reading code.

    Indeed even if the import statement was at the top of the function (or class) as many would place it, paging up and back down would be required.

    Displaying the Gnome version number will rarely be done so the import at top of file introduces unnecessary startup lag.

    0 讨论(0)
  • 2020-11-22 03:45

    Putting the import statement inside of a function can prevent circular dependencies. For example, if you have 2 modules, X.py and Y.py, and they both need to import each other, this will cause a circular dependency when you import one of the modules causing an infinite loop. If you move the import statement in one of the modules then it won't try to import the other module till the function is called, and that module will already be imported, so no infinite loop. Read here for more - effbot.org/zone/import-confusion.htm

    0 讨论(0)
  • 2020-11-22 03:45

    I would like to mention a usecase of mine, very similar to those mentioned by @John Millikin and @V.K. :

    Optional Imports

    I do data analysis with Jupyter Notebook, and I use the same IPython notebook as a template for all analyses. In some occasions, I need to import Tensorflow to do some quick model runs, but sometimes I work in places where tensorflow isn't set up / is slow to import. In those cases, I encapsulate my Tensorflow-dependent operations in a helper function, import tensorflow inside that function, and bind it to a button.

    This way, I could do "restart-and-run-all" without having to wait for the import, or having to resume the rest of the cells when it fails.

    0 讨论(0)
提交回复
热议问题