Best practices with import?

Tom Bryan tbryan at python.net
Sat Jul 28 12:13:37 EDT 2001


I've been using Python off and on for over two years.  I've been teaching 
my wife Python, and she recently asked me how/where she should use 
import.  I realized that I have no idea what the best idioms are and what 
arguments support doing it one way or another.  For example, if I have a 
module that uses string, should I put an 'import string' at the top of the 
module?  Should every function that needs the module import it?  Sometimes 
I see modules that use both imports at the module level and at the function
level.  Why would I want to do that?  Finally, I vaguely remember that 
one of the bots once explained why I might want to do something like this 

def foo( arg ):
    import string
    # Lots of cool code omitted
    del string
    return val

but now I can't remember why, and I can't find the thread.

So, what are the "best practices" when using import (other than the simple 
"don't do a from module import *"?

i'll-even-use-the-answers-to-improve-python's-faq-ly yours
---Tom




More information about the Python-list mailing list