import X between submodules in a package

Donn Ingle donn.ingle at gmail.com
Wed Dec 19 02:24:00 EST 2007


Hi, I'm sure this is a FAQ, but I have just not found clarity on the
web/docs.

(using monospaced type to show the tree)
trunk:$ tree
.
fp
|-- fontypython
|   |-- __init__.py
|   |-- cli.py
|   |-- config.py

(I start it all with ./fp)

fp says:
import cli

cli.py says:
import os
import config

config.py says:
print os.environ['HOME']

I get a NameError in config.py

If I add 'import os' to config.py all is well.

So, I guess I am confused about the 'scope' of what gets imported where. I
am thinking that if one module (py file) does *import os* something and
*then* imports another module - the second module should have access to os
too?
 I imagine myself "standing" inside cli.py at this point and saying "well I
can see os, and I'm bringing config *into* this space, so they should be
able to see os too."

How do I structure things so that I don't have endless repetitions of import
in every py file within my package directory?

\d





More information about the Python-list mailing list