tags:

views:

699

answers:

9

Suppose I have a relatively long module, but need an external module or method only once.

Is it considered ok to import that method or module in the middle of the module?

Or should imports only be in the first part of the module.

Example:

import string, pythis, pythat
...
...
...
...
def func():
     blah
     blah 
     blah
     from pysomething import foo
     foo()
     etc
     etc 
     etc
...
...
...

Please justify your answer and add links to PEPs or relevant sources

+7  A: 

It is considered "Good Form" to group all imports together at the start of the file.

Modules can import other modules. It is customary but not required to place all import statements at the beginning of a module (or script, for that matter). The imported module names are placed in the importing module’s global symbol table.

From here: http://docs.python.org/tutorial/modules.html

Frozenskys
+2  A: 

It's generally considered bad practice, but sometimes it's unavoidable (say when you have to avoid a circular import).

An example of a time when it is necessary: I use Waf to build all our code. The system is split into tools, and each tool is implemented in it's own module. Each tool module can implent a detect() method to detect if the pre-requisites are present. An example of one of these may do the following:

def detect(self):
    import foobar

If this works correctly, the tool is usable. Then later in the same module the foobar module may be needed, so you would have to import it again, at function level scope. Clearly if it was imported at module level things would blow up completely.

jkp
The import inside the def is certainly avoidable. We use `try: import x; except ImportError` logic blocks to keep all the imports up front.
S.Lott
+17  A: 

PEP 8 authoritatively states:

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

PEP 8 should be the basis of any "in-house" style guide, since it summarizes what the core Python team has found to be the most effective style, overall (and with individual dissent of course, as on any other language, but consensus and the BDFL agree on PEP 8).

Alex Martelli
Alex: it is "best-practice" but not always avoidable. See the example I gave above.
jkp
@jkp: What does "above" mean in stack overflow?
S.Lott
@jkp, S.Lott commented on your answer and showed why it IS avoidable in that example. The only examples I can think of would be using dynamically generated module names, thus `__import__`, not plain import -- in *those* cases you can't import until you know the name of the module you're importing, but then you're not using a plain `import` on it.
Alex Martelli
@S.Lott "Below" :)
Draemon
+3  A: 

if the imported module is infrequently used and the import is expensive the in-the-middle-import is ok.

otherwise is it wise, to follow Alex Martellis suggestion

Blauohr
+2  A: 

PEP8:

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

It is not bad practice to have scopped imports. So that the import applies only to the function you used it in.

I think the code would be more readable though if the imports where grouped together at the top of the block or if you want it globally at the top of the file.

Brian R. Bondy
+2  A: 

Well, I think it is a good practice to group all imports together at start of file since everyone knows where to look if want to know which libs are loaded

jab
+6  A: 

There was a detailed discussion of this topic on the Python mailing list in 2001:

http://mail.python.org/pipermail/python-list/2001-July/097838.html

The URL appears to have changed

http://mail.python.org/pipermail/python-list/2001-July/097194.html

ire_and_curses
Upvoting this for providing link to a discussion, rather than just "PEP 8 says so". I would say the "meat" of it starts several messages into the thread (http://mail.python.org/pipermail/python-list/2001-July/097866.html).
John Y
+3  A: 

95% of the time, you should put all your imports at the top of the file. One case where you might want to do a function-local import is if you have to do it in order to avoid circular imports. Say foo.py imports bar.py, and a function in bar.py needs to import something from foo.py. If you put all your imports at the top, you could have unexpected problems importing files that rely on information that hasn't been compiled yet. In this case, having a function local import can allow your code to hold off on importing the other module until its code has been fully compiled, and you call the relevant function.

However, it looks like your use-case is more about making it clear where foo() is coming from. In this case, I would far prefer one of two things:

First, rather than

from prerequisite import foo

import prerequisite directly, and later on refer to it as prerequisite.foo. The added verbosity pays itself back in spades through increased code transparency.

Alternatively, (or in conjunction with the above) if it's really such a long distance between your import and the place it's being used, it may be that your module is too big. The need for an import that nothing else uses might be an indication of a place where your code could stand to be refactored into a more manageably-sized chunk.

jcdyer
Circularity can be fixed through decomposition. Conditional imports are -- at best -- a clunky workaround. I'd say it's 99.7% of the time and the 0.3% is "until you can decompose to fix the circularity".
S.Lott
+5  A: 

Everyone else has already mentioned the PEPs, but also take care to not have import statements in the middle of critical code. At least under Python 2.6, there are several more bytecode instructions required when a function has an import statement.

>>> def f():
    from time import time
    print time()

>>> dis.dis(f)
  2           0 LOAD_CONST               1 (-1)
              3 LOAD_CONST               2 (('time',))
              6 IMPORT_NAME              0 (time)
              9 IMPORT_FROM              0 (time)
             12 STORE_FAST               0 (time)
             15 POP_TOP             

  3          16 LOAD_FAST                0 (time)
             19 CALL_FUNCTION            0
             22 PRINT_ITEM          
             23 PRINT_NEWLINE       
             24 LOAD_CONST               0 (None)
             27 RETURN_VALUE

>>> def g():
    print time()

>>> dis.dis(g)
  2           0 LOAD_GLOBAL              0 (time)
              3 CALL_FUNCTION            0
              6 PRINT_ITEM          
              7 PRINT_NEWLINE       
              8 LOAD_CONST               0 (None)
             11 RETURN_VALUE
Mark Rushakoff