question about importing a package

Matt matt at woodridgeadvisors.com
Thu Dec 6 11:50:59 EST 2012


It works now. Steven and Alex, thanks for your help!

I ended up leaving sample.py and foo.py and bar.p the way they were, and in __init__.py putting:

from foo import *
from bar import *

So my mistake was not importing the foo and bar modules into sub_one/__init__.py.

I also see how the __all__ array helps me control what gets imported. I can leave it out of __init__.py, and everything gets imported. So my three lessons are:

1) "from X import *" will look for an __all__ list in module X, or in __init__.py if X is a package instead of a module, and import only what is in that list. Module names are different than function names in that list.
2) if __all__ is not defined, "from X import *' will import everything in X's namespace
3) __init__.py acts like just another module, so you have to import the package contents that you want into it before you import the package into your code

Thanks again for the help!



More information about the Python-list mailing list