Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.05; 'skip:` 10': 0.05; 'sys': 0.05; 'assign': 0.07; 'rename': 0.07; 'python': 0.09; '(without': 0.09; 'expected.': 0.09; 'fails.': 0.09; 'filesystem': 0.09; 'happen?': 0.09; '2.7': 0.13; 'resulting': 0.13; '(say': 0.16; '3.2,': 0.16; 'assigns': 0.16; 'variable.': 0.16; 'work.\xa0': 0.16; 'package.': 0.17; 'saying': 0.18; 'load': 0.19; 'module': 0.19; 'import': 0.21; 'work.': 0.23; 'statement': 0.23; 'seems': 0.23; 'tried': 0.25; 'skip:" 20': 0.26; 'used,': 0.27; 'module.': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'behavior.': 0.29; 'skip:& 10': 0.29; 'probably': 0.29; 'knows': 0.30; 'skip:& 30': 0.30; 'code': 0.31; 'file': 0.32; 'instead,': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'skip:& 20': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'loaded': 0.36; 'modules': 0.36; 'correctly': 0.37; 'why': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'copying': 0.38; 'files': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'first': 0.61; 'natural': 0.65; 'results': 0.65; 'subject:Import': 0.84; 'suboptimal.': 0.84; 'affected.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=9wooS1ut0ZVbyy79CKrTCrWE8q3kzdK8OVjj0jiic9U=; b=ejVq5KTMq+El8RErFsJXO44boeQ1LsB9Ve96709rDcpdgJJh5d8Oe+1Ng/K/K0ldR0 F2HYMryEWVlvGwqxgVT5pYmSS4Cg+efKE/k9qLWApcJhhWP5UgeyXnik3cV3wUWRO3wF 0vDvRONAYQNMJnqltEVslBih3PDapoFCPmp3gYziHzOLgUimlLo/lw2W6gFw+pb1Ifz/ Jicc4rRZnS9/h1L1ijxwp4iB4P7hLOOAnMjGYcTxur4QGu2Igm/xrl1cuXxauZ05Z8Jb a8pYwWQdqHrvmci7JYCqGIlNqFOXV7yqs+k2Mr1NoIoLdqSzwsGcD+jofojbpZv5kAsu oEkg== MIME-Version: 1.0 X-Received: by 10.229.106.134 with SMTP id x6mr1182981qco.132.1360565436132; Sun, 10 Feb 2013 22:50:36 -0800 (PST) Date: Mon, 11 Feb 2013 14:50:36 +0800 Subject: Import redirects From: Isaac To To: python-list@python.org Content-Type: multipart/alternative; boundary=002354332a3a83af7d04d56d5373 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360565439 news.xs4all.nl 6865 [2001:888:2000:d::a6]:46529 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38648 --002354332a3a83af7d04d56d5373 Content-Type: text/plain; charset=ISO-8859-1 I have a package (say "foo") that I want to rename (say, to "bar"), and for compatibility reasons I want to be able to use the old package name to refer to the new package. Copying files or using filesystem symlinks is probably not the way to go, since that means any object in the modules of the package would be duplicated, changing one will not cause the other to be updated. Instead, I tried the following as the content of `foo/__init__.py`: import sys import bar sys.modules['foo'] = bar To my surprise, it seems to work. If I `import foo` now, the above will cause "bar" to be loaded and be used, which is expected. But even if I `import foo.baz` now (without first `import foo`), it will now correctly import "bar.baz" in its place. 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. I tested it with both Python 2.7 and Python 3.2, and I see exactly the same behavior. Anyone knows why that happen? My current work-around is to use the above code only for modules and not for packages, which is suboptimal. Anyone knows a better work-around? --002354332a3a83af7d04d56d5373 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
I have a package (say "foo") that= I want to rename (say, to "bar"), and for compatibility reasons = I want to be able to use the old package name to refer to the new package.= =A0 Copying files or using filesystem symlinks is probably not the way to g= o, since that means any object in the modules of the package would be dupli= cated, changing one will not cause the other to be updated.=A0 Instead, I t= ried the following as the content of `foo/__init__.py`:

=A0=A0=A0 import sys
=A0=A0=A0 import bar
=A0=A0=A0 sys.modules[&= #39;foo'] =3D bar

To my surprise, it seems to work.=A0 If = I `import foo` now, the above will cause "bar" to be loaded and b= e used, which is expected.=A0 But even if I `import foo.baz` now (without f= irst `import foo`), it will now correctly import "bar.baz" in its= place.

Except one thing: it doesn't really work.=A0 If I `import foo= .baz.mymod` now, and if in "bar.baz.mymod" there is a statement `= import bar.baz.depmod`, then it fails.=A0 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.=A0 But it fails to assi= gn the module object of "mymod" into the "bar.baz" modu= le.=A0 So after `import foo.baz.mymod`, `foo.baz.mymod` results in an Attri= buteError saying 'module' object has no attribute 'mymod'.= =A0 The natural `import bar.baz.mymod` is not affected.

I tested it with both Python 2.7 and Python 3.2, and I see e= xactly the same behavior.

Anyone knows why that hap= pen?=A0 My current work-around is to use the above code only for modules an= d not for packages, which is suboptimal.=A0 Anyone knows a better work-arou= nd?
--002354332a3a83af7d04d56d5373--