Where to "import"?

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Mar 8 16:08:29 EST 2007


Paulo da Silva <psdasilvaX at esotericaX.ptX> writes:

> Hi!
>
> If I have two files .py such as
>
> m.py
> 	from c import *

Best done as either

    import c    # then use 'x = c.c()'

or

    from c import c

Using 'from foo import *' leads to names appearing in your current
namespace that are difficult to track to their origin by reading the
source code.

> both using os module where should I put the "import os"? In both
> files?

Yes. "import os" does two things of note:

  - iff the module is not currently loaded and executed, do so
  - bind the module object to the name "os" in the current namespace

The first step means that you're not losing anything by importing the
module wherever it's needed. The second means the module is available
for use within the current namespace.

-- 
 \              "When cryptography is outlawed, bayl bhgynjf jvyy unir |
  `\                                           cevinpl."  -- Anonymous |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list