Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; 'example:': 0.03; 'subject:module': 0.04; 'subject:using': 0.04; 'imports': 0.07; 'pep': 0.07; 'though.': 0.07; '21,': 0.09; 'pm,': 0.10; 'this:': 0.10; 'wrote:': 0.14; 'defined': 0.14; '"import': 0.16; 'discourages': 0.16; 'subject:import': 0.16; 'subject:package': 0.16; 'then:': 0.16; '\xa0to': 0.16; '\xa0would': 0.16; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'objects': 0.23; 'received:209.85.161.46': 0.23; 'received:mail- fx0-f46.google.com': 0.23; 'modules': 0.26; 'preferred': 0.26; 'received:209.85.161': 0.26; 'message-id:@mail.gmail.com': 0.28; 'sat,': 0.29; 'subject:?': 0.29; 'import': 0.29; 'modules,': 0.30; 'hi,': 0.31; 'it.': 0.31; 'to:addr:python-list': 0.33; 'several': 0.36; 'created': 0.36; 'received:google.com': 0.37; 'something': 0.37; 'received:209.85': 0.37; 'subject:from': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'absolute': 0.40; 'subject:, ': 0.60 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=soq2P2fkgvyWoQUgLs52CJ4HZS5YcZG54zJZzIgs4Rk=; b=oRjwc1tQUHx0AuECF2cjdtn0rO0Z+cpb8KTYgVlf1iJh1UsgzVw8Ugop0PTgUjBeph cvwbhcwgnjhv50VxniVGVRW45QTuCxSAs7TZBdWKsP3BUo/IFzGnnSgpFoBWyIM1ixR9 YF/sSalzdUlVvGkKCnoLvfXiRcL/N+DFrAzrU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=IZRDx1yR/LT2N4pMwBt+sfgFBerAGr2MY+4/PrdHGKIwQzkq3gfQyVrq211zkG6LJY DFBmiVyv+cy7GBgNSgQVHh5BJwocLSzO99llJr0Yaxr6dXH9ojOOT8/vKViIOPbndQ9t Oh17q9mFuQOnPE3LRqoBvHKX0OYhaMAWLcD3I= MIME-Version: 1.0 In-Reply-To: <4dd81254$0$49046$e4fe514c@news.xs4all.nl> References: <4dd81254$0$49046$e4fe514c@news.xs4all.nl> From: Ian Kelly Date: Sat, 21 May 2011 14:00:12 -0600 Subject: Re: referring to package scope from module, using relative import? To: Python Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 39 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306008043 news.xs4all.nl 49177 [::ffff:82.94.164.166]:33812 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5935 On Sat, May 21, 2011 at 1:28 PM, Irmen de Jong wro= te: > Hi, > > I have a package with several modules in it. The package also has some ob= jects created > in the package scope (done in the package __init__.py). > > Is it possible to access those package scope objects from the modules, wi= th relative > imports or something? So that I don't have to import the package itself i= n its own > submodules? > > > Example: > > A/ > =A0 __init__.py =A0 =A0-> =A0creates A.something > =A0 thing.py =A0 =A0 =A0 -> =A0needs to do "import A" =A0to access A.some= thing > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0would like to not have to impo= rt A You can do the relative import like this: from . import something Or if something were defined in A/otherthing.py, then: from .otherthing import something Note that PEP 8 discourages relative imports and encourages absolute imports, though. This would be the preferred way to do it: from A import something Cheers, Ian