Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'importing': 0.04; 'attribute': 0.05; 'skip:` 10': 0.05; 'sys': 0.05; 'assign': 0.07; 'fails.': 0.09; 'imports': 0.09; 'inserted': 0.09; 'modules.': 0.09; 'resulting': 0.13; 'assigns': 0.16; 'benjamin': 0.16; 'intended.': 0.16; 'suggestion.': 0.16; 'to:name:python list': 0.16; 'variable.': 0.16; 'mon,': 0.16; 'wrote:': 0.17; "shouldn't": 0.17; 'saying': 0.18; '>>>': 0.18; 'feb': 0.19; 'load': 0.19; 'module': 0.19; 'email addr:gmail.com>': 0.20; 'import': 0.21; '>>>': 0.22; '\xa0if': 0.22; '\xa0so': 0.22; 'work.': 0.23; '>': 0.23; 'statement': 0.23; 'this:': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; 'wrote': 0.26; 'skip:" 20': 0.26; 'guess': 0.27; 'module.': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'skip:& 10': 0.29; "skip:' 10": 0.30; 'skip:& 30': 0.30; 'file': 0.32; "skip:' 20": 0.32; '11,': 0.33; 'to:addr:python-list': 0.33; 'skip:& 20': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'false': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'anything': 0.36; 'bad': 0.37; 'correctly': 0.37; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'think': 0.40; 'your': 0.60; 'natural': 0.65; 'results': 0.65; 'attractive': 0.78; 'funny': 0.78; '2013': 0.84; 'bite': 0.84; 'isaac': 0.84; 'oscar': 0.84; 'subject:Import': 0.84; 'affected.': 0.91; 'magical': 0.93; 'hand,': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=rhogTgGbsP6LRPWu73GzICU0uEPKoDX1VyydJQyIQkc=; b=qK2trju+Xv7ocOpXCt5iR37UZz/4bxvOBwZ1LGRMf6JI8f/MvVkGVGUJGJi6bhhlZb BwpVshk0yle08TTSaoc5dB65vbhImWa/HWsz2jAh7zec1IxWkjTBiEYl2uGy0su0j9ng wXzwPTtJ5tt2AjXq4ZGq/PktJk69eVVX2/z11+Dxc6deUBh6uT0PhymLgIbeGpYdG6K4 D37ca9aQC8haQDT8fsNKv4/GMuj0GnGeQ/8kfQ4wTN0feW9AkC1EUaaK3BnTg/BzSkXF lHGVTksxHfUVPAUC7bI0746G/n87zEO9lp+je1kecHkr/+nXz0ihcCOgpWHF80dZAcPg eWpQ== MIME-Version: 1.0 X-Received: by 10.229.76.89 with SMTP id b25mr1480574qck.31.1360637458020; Mon, 11 Feb 2013 18:50:58 -0800 (PST) In-Reply-To: References: Date: Tue, 12 Feb 2013 10:50:57 +0800 Subject: Re: Import redirects From: Isaac To To: Python List Content-Type: multipart/alternative; boundary=00235429dfc45a7a1204d57e1872 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: 136 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360637461 news.xs4all.nl 6861 [2001:888:2000:d::a6]:39188 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38717 --00235429dfc45a7a1204d57e1872 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Feb 11, 2013 at 8:27 PM, Oscar Benjamin wrote: > On 11 February 2013 06:50, Isaac To 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 >>> import foo.baz.mymod >>> bar.baz.mymod 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 --00235429dfc45a7a1204d57e1872 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On Mon, Feb 11, 2013 at 8:27 PM, Oscar Benjamin <= ;oscar.j.be= njamin@gmail.com> wrote:
On 11 F= ebruary 2013 06:50, Isaac To <isaa= c.to@gmail.com> wrote:
> Except one thing: it doesn't really work. =A0If I `import foo.baz.= mymod` now,
> and if in "bar.baz.mymod" there is a statement `import bar.b= az.depmod`, then
> it fails. =A0It 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. =A0So after `import foo.baz.mymod`, `foo.baz.mymod` results in= an
> AttributeError saying 'module' object has no attribute 'my= mod'. =A0The natural
> `import bar.baz.mymod` is not affected.

My guess is that you have two copies of the module object bar.baz wit= h
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'] =3D bar.baz

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

=A0=A0=A0 import sys
=A0=A0=A0 import bar
=A0= =A0=A0 import bar.baz
=A0=A0=A0 sys.modules['foo.baz'] =3D bar.b= az
=A0=A0=A0 sys.modules['foo'] =3D bar

One funny effect = I get is this:

=A0=A0=A0 >>> import bar.baz.mymod
=A0=A0=A0 >>> bar.baz.mymod
=A0=A0=A0 <module 'bar.baz.m= ymod' from 'bar/baz/mymod.pyc'>
=A0=A0=A0 >>> im= port foo.baz.mymod
=A0=A0=A0 >>> bar.baz.mymod
=A0=A0=A0 <= ;module 'foo.baz.mymod' from 'bar/baz/mymod.pyc'>

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

=A0=A0=A0 >>> import bar.baz.mymod as bbm
=A0= =A0=A0 >>> import foo.baz.mymod as fbm
=A0=A0=A0 >>> b= bm is fbm
=A0=A0=A0 False

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

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

Regards,
<= /div>
Isaac
--00235429dfc45a7a1204d57e1872--