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


Groups > comp.lang.python > #5935

Re: referring to package scope from module, using relative import?

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 <ian.g.kelly@gmail.com>
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 <ian.g.kelly@gmail.com>
Date Sat, 21 May 2011 14:00:12 -0600
Subject Re: referring to package scope from module, using relative import?
To Python <python-list@python.org>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1888.1306008043.9059.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sat, May 21, 2011 at 1:28 PM, Irmen de Jong <irmen.NOSPAM@xs4all.nl> wrote:
> Hi,
>
> I have a package with several modules in it. The package also has some objects created
> in the package scope (done in the package __init__.py).
>
> Is it possible to access those package scope objects from the modules, with relative
> imports or something? So that I don't have to import the package itself in its own
> submodules?
>
>
> Example:
>
> A/
>   __init__.py    ->  creates A.something
>   thing.py       ->  needs to do "import A"  to access A.something
>                      would like to not have to import 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

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


Thread

referring to package scope from module, using relative import? Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2011-05-21 21:28 +0200
  Re: referring to package scope from module, using relative import? Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-21 14:00 -0600
    Re: referring to package scope from module, using relative import? Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2011-05-21 23:32 +0200

csiph-web