Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

PEP 8 states:

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

However if the class/method/function that I am importing is only used in rare cases, surely it is more efficient to do the import when it is needed?

Isn't this:

class SomeClass(object):      def not_often_called(self)         from datetime import datetime         self.datetime = datetime.now() 

more efficient than this?

from datetime import datetime  class SomeClass(object):      def not_often_called(self)         self.datetime = datetime.now() 
like image 258
Adam J. Forster Avatar asked Sep 24 '08 17:09

Adam J. Forster


People also ask

Does the order of imports matter in Python?

Import order does not matter. If a module relies on other modules, it needs to import them itself. Python treats each . py file as a self-contained unit as far as what's visible in that file.

What is correct way to import a Python module?

Importing Modules To make use of the functions in a module, you'll need to import the module with an import statement. An import statement is made up of the import keyword along with the name of the module. In a Python file, this will be declared at the top of the code, under any shebang lines or general comments.

Which is the correct way to import the module in your program?

You need to use the import keyword along with the desired module name. When interpreter comes across an import statement, it imports the module to your current program. You can use the functions inside a module by using a dot(.) operator along with the module name.

In what order will the Python import statement look for modules?

In fact, such references are so common that the import statement first looks in the containing package before looking in the standard module search path. When you run python -m bar , bar is a top level module, i.e. it has no containing module. In that case import os goes to through sys. path .


2 Answers

Module importing is quite fast, but not instant. This means that:

  • Putting the imports at the top of the module is fine, because it's a trivial cost that's only paid once.
  • Putting the imports within a function will cause calls to that function to take longer.

So if you care about efficiency, put the imports at the top. Only move them into a function if your profiling shows that would help (you did profile to see where best to improve performance, right??)


The best reasons I've seen to perform lazy imports are:

  • Optional library support. If your code has multiple paths that use different libraries, don't break if an optional library is not installed.
  • In the __init__.py of a plugin, which might be imported but not actually used. Examples are Bazaar plugins, which use bzrlib's lazy-loading framework.
like image 193
John Millikin Avatar answered Oct 08 '22 16:10

John Millikin


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

like image 42
Moe Avatar answered Oct 08 '22 16:10

Moe