PEP 8 exegetics: conditional imports?

Albert Hopkins marduk at
Fri Aug 7 20:18:51 CEST 2009

On Fri, 2009-08-07 at 16:50 +0000, kj wrote:
> Conditional imports make sense to me, as in the following example:
> def foobar(filename):
>     if os.path.splitext(filename)[1] == '.gz':
>         import gzip
>         f =
>     else:
>         f = file(filename)
>     # etc.
> And yet, quoth PEP 8:
>     - Imports are always put at the top of the file, just after any module
>       comments and docstrings, and before module globals and constants.
> ...which seems to condemn conditional imports unequivocally. 
> Then again, the PEP 8 scriptures do not explicitly mention conditional
> imports at all, as far as I can tell, which leaves open the
> possibility that they are still righteous. 
> In fact, venerable passages in the Python standard library source
> code, if properly interpreted, can be seen to carry out conditional
> imports, such as this fragment recovered from
>         if a is None:
>             try:
>                 a = long(_hexlify(_urandom(16)), 16)
>             except NotImplementedError:
>                 import time
>                 a = long(time.time() * 256) # use fractional seconds
> Or even more clearly, this one from test/
> if __name__ == '__main__':
>     import sys

I can't speak for others... but generally you can "tell" when an import
belongs at the top of a module and when you need to make exceptions.  I
would say that, as a general rule, they go up top (easier to identify
what external dependencies a module have).  There are, of course,

      * Exceptions and other not-often-executed blocks
      * Inside test functions (where the imported modules are only used
        by tests
      * Inside main() functions where the imported modules are only used
        if the module is run as a script
      * When startup-time is important. Often it's necessary to have you
        module up and running in an instant and import "expensive"
        modules as-needed.
      * "Dynamic" import such as plugins, etc.

Of course, like everything else in PEP 8, it's meant as a "guide" and
not as an "order".  YMMV.


More information about the Python-list mailing list