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


Groups > comp.lang.python > #5933 > unrolled thread

referring to package scope from module, using relative import?

Started byIrmen de Jong <irmen.NOSPAM@xs4all.nl>
First post2011-05-21 21:28 +0200
Last post2011-05-21 23:32 +0200
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  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

#5933 — referring to package scope from module, using relative import?

FromIrmen de Jong <irmen.NOSPAM@xs4all.nl>
Date2011-05-21 21:28 +0200
Subjectreferring to package scope from module, using relative import?
Message-ID<4dd81254$0$49046$e4fe514c@news.xs4all.nl>
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


I think it's something simple I'm missing...

Irmen


[toc] | [next] | [standalone]


#5935

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-05-21 14:00 -0600
Message-ID<mailman.1888.1306008043.9059.python-list@python.org>
In reply to#5933
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

[toc] | [prev] | [next] | [standalone]


#5937

FromIrmen de Jong <irmen.NOSPAM@xs4all.nl>
Date2011-05-21 23:32 +0200
Message-ID<4dd82f83$0$49042$e4fe514c@news.xs4all.nl>
In reply to#5935
On 21-5-2011 22:00, Ian Kelly wrote:

> 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

Right. I got rid of the silly relative import stuff. As an added bonus, this makes my
original question irrelevant :)

Irmen

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web