Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34429

Re: question about importing a package

From Terry Reedy <tjreedy@udel.edu>
Subject Re: question about importing a package
Date 2012-12-06 15:44 -0500
References <4e72a337-07a2-4759-9960-3e8dab7f1e5c@googlegroups.com> <116c6e9c-88d1-4f16-9d81-8833b3be519d@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.589.1354826674.29569.python-list@python.org> (permalink)

Show all headers | View raw


On 12/6/2012 11:50 AM, Matt wrote:
> 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

... that does not have a leading underscore. This is why there are 
things like

import sys as _sys
from itertools import chain as _chain

in the stdlib when the module author does not define __all__.

> 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


-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

question about importing a package Matt <matt@woodridgeadvisors.com> - 2012-12-05 20:58 -0800
  Re: question about importing a package Chris Angelico <rosuav@gmail.com> - 2012-12-06 16:18 +1100
  Re: question about importing a package alex23 <wuwei23@gmail.com> - 2012-12-05 22:30 -0800
  Re: question about importing a package Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-06 06:54 +0000
  Re: question about importing a package Matt <matt@woodridgeadvisors.com> - 2012-12-06 08:50 -0800
    Re: question about importing a package Terry Reedy <tjreedy@udel.edu> - 2012-12-06 15:44 -0500

csiph-web