Where to import modules?

Batista, Facundo FBatista at uniFON.com.ar
Fri Sep 26 14:43:15 CEST 2003

#- I have a "style" question: Sometimes, modules will only be used in a 
#- particular, optional, part of a program (function, class), 
#- that will not 
#- always be used when the application is run. So I think it is 
#- better to 
#- import them only there, not on the top of the file (together 
#- with the 
#- other imports). Is that okay, or are there good reasons for 
#- not doing so?

>From PEP-8 (Style Guide for Python Code):

    - Imports are always put at the top of the file, just after any
      module comments and docstrings, and before module globals and
      constants.  Imports should be grouped, with the order being

      1. standard library imports
      2. related major package imports (i.e. all email package imports next)
      3. application specific imports

      You should put a blank line between each group of imports.

.	Facundo

More information about the Python-list mailing list