The good "import" convention?

Joshua Marshall josh at nowhere.com
Mon Feb 25 11:42:37 EST 2002


Jason Orendorff <jason at jorendorff.com> wrote:

> A single set of imports, at the top of each file, is better,
> because that's the convention.  "Prefer the standard to the
> offbeat."

Convention is useful in decision-making as a last resort.

Generally, where I import depends on a few things.  If only one or two functions
in my module use the functionality of the imported module, then I prefer to have
the imports inside my function definitions.

Having the import statements inside of function definitions also avoids some
circularity issues, if you have interdependent modules.  (Let's not turn that
into a design flamewar.  Sometimes it's useful to have circularly-dependent
modules.)

Otherwise, if the imported module is used heavily, I usually put the import
statement at the top of my module.



More information about the Python-list mailing list