Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'importing': 0.04; 'sys': 0.05; 'subject:question': 0.08; '__all__': 0.09; 'defined,': 0.09; 'imported.': 0.09; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; '"from': 0.16; '__all__.': 0.16; '__init__.py': 0.16; '_sys': 0.16; 'itertools': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'underscore.': 0.16; 'wrote:': 0.17; 'module,': 0.17; 'jan': 0.18; 'module': 0.19; 'are:': 0.20; 'define': 0.20; 'import': 0.21; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'leave': 0.26; 'am,': 0.27; 'header:X-Complaints- To:1': 0.28; 'lessons': 0.29; 'steven': 0.29; 'array': 0.29; 'function': 0.30; 'code': 0.31; 'gets': 0.32; 'to:addr:python- list': 0.33; 'another': 0.33; 'thanks': 0.34; 'list': 0.35; 'there': 0.35; 'list.': 0.35; 'received:org': 0.36; 'modules': 0.36; 'author': 0.37; 'does': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'leading': 0.61; 'leaving': 0.62; 'helps': 0.63; 'different': 0.63; 'acts': 0.71; 'received:fios.verizon.net': 0.84; 'were,': 0.84; 'mistake': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: question about importing a package Date: Thu, 06 Dec 2012 15:44:10 -0500 References: <4e72a337-07a2-4759-9960-3e8dab7f1e5c@googlegroups.com> <116c6e9c-88d1-4f16-9d81-8833b3be519d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20121026 Thunderbird/16.0.2 In-Reply-To: <116c6e9c-88d1-4f16-9d81-8833b3be519d@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354826674 news.xs4all.nl 6854 [2001:888:2000:d::a6]:36757 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34429 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