Where to place imports

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Jan 24 00:46:04 CET 2009

On Fri, 23 Jan 2009 17:48:19 +0100, Diez B. Roggisch wrote:

> The only valid reason for doing imports inside functions is if you
> otherwise get into circular dependency hell, or have modules that need
> some manipulation of the sys.path before they actually can be imported.

Or if you want to make it clear that only a single function uses the 
imported module, or if you wish to avoid a potentially expensive import 
until the last possible moment when you actually need it, or if you're 
finicky about namespace pollution, or if you just like to put your 
imports inside functions because you're weird and never use threads.

All of these are valid reasons. Whether they will give you the love and 
respect of your fellow Python programmers is another question.


More information about the Python-list mailing list