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


Groups > comp.lang.python > #38717

Re: Import redirects

References <CAOArY3VQFytYUZrKxxRHOYQfxP85=P6yT5b83wzpJBMwK4hKGQ@mail.gmail.com> <CAHVvXxTc-ciO_5zVWcaRCDE1WHvjuBUAe4YSQ9O=_U8G_-FwKA@mail.gmail.com>
Date 2013-02-12 10:50 +0800
Subject Re: Import redirects
From Isaac To <isaac.to@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1677.1360637461.2939.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On Mon, Feb 11, 2013 at 8:27 PM, Oscar Benjamin
<oscar.j.benjamin@gmail.com>wrote:

> On 11 February 2013 06:50, Isaac To <isaac.to@gmail.com> wrote:
> > Except one thing: it doesn't really work.  If I `import foo.baz.mymod`
> now,
> > and if in "bar.baz.mymod" there is a statement `import bar.baz.depmod`,
> then
> > it fails.  It correctly load the file "bar/baz/depmod.py", and it assigns
> > the resulting module to the package object bar.baz as the "depmod"
> variable.
> > But it fails to assign the module object of "mymod" into the "bar.baz"
> > module.  So after `import foo.baz.mymod`, `foo.baz.mymod` results in an
> > AttributeError saying 'module' object has no attribute 'mymod'.  The
> natural
> > `import bar.baz.mymod` is not affected.
>
> My guess is that you have two copies of the module object bar.baz with
> one under the name foo.baz and the other under the name bar.baz. mymod
> is inserted at bar.baz but not at foo.baz. I think a solution in this
> case would be to have your foo/__init__.py also import the subpackage
> 'bar.baz' and give it both names in sys.modules:
>
> import bar.baz
> sys.modules['foo.baz'] = bar.baz
>

Thanks for the suggestion.  It is indeed attractive if I need only to
pre-import all the subpackage and not to redirect individual modules.  On
the other hand, when I actually try this I found that it doesn't really
work as intended.  What I actually wrote is, as foo/__init__.py:

    import sys
    import bar
    import bar.baz
    sys.modules['foo.baz'] = bar.baz
    sys.modules['foo'] = bar

One funny effect I get is this:

    >>> import bar.baz.mymod
    >>> bar.baz.mymod
    <module 'bar.baz.mymod' from 'bar/baz/mymod.pyc'>
    >>> import foo.baz.mymod
    >>> bar.baz.mymod
    <module 'foo.baz.mymod' from 'bar/baz/mymod.pyc'>

By importing foo.baz.mymod, I change the name of the module from
"bar.baz.mymod" to "foo.baz.mymod".  If that is not bad enough, I also see
this:

    >>> import bar.baz.mymod as bbm
    >>> import foo.baz.mymod as fbm
    >>> bbm is fbm
    False

Both effects are there even if bar/baz/mymod.py no longer `import
bar.baz.depmod`.

It looks to me that package imports are so magical that I shouldn't do
anything funny to it, as anything that seems to work might bite me a few
minutes later.

Regards,
Isaac

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


Thread

Re: Import redirects Isaac To <isaac.to@gmail.com> - 2013-02-12 10:50 +0800

csiph-web