question about importing a package

Matt matt at woodridgeadvisors.com
Thu Dec 6 05:58:46 CET 2012


I have a directory structure that looks like this:

sample.py
sub_one/
      __init__.py     # defines only the list    __all__ = ['foo', 'bar']
      foo.py           # defines the function in_foo()  
      bar.py           # defines the function in_bar()

In sample.py, I have this command at the top:

from sub_one import *

I can't refer to in_foo() and in_bar() without prefacing them with the module names. I.e. foo.in_foo() and bar.in_bar() work, but I want to import them in the __main__ namespace of sample.py and refer to them as just in_foo() and in_bar(). I know this is frowned upon, but for my purposes it is the best choice. I have about 30 modules in my package (foos and bars) and I don't want 30 lines at the top of each file that uses this package. What am I doing wrong?

Thanks,

Matt



More information about the Python-list mailing list