Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38668
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <oscar.j.benjamin@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; 'attribute': 0.05; 'skip:` 10': 0.05; 'sys': 0.05; 'assign': 0.07; 'rename': 0.07; '(without': 0.09; 'expected.': 0.09; 'fails.': 0.09; 'filesystem': 0.09; 'inserted': 0.09; 'portable': 0.09; 'symlink': 0.09; 'cc:addr:python-list': 0.10; 'resulting': 0.13; '(say': 0.16; 'assigns': 0.16; 'cc:name:python list': 0.16; 'variable.': 0.16; 'wrote:': 0.17; 'package.': 0.17; 'saying': 0.18; 'solution.': 0.18; 'load': 0.19; 'module': 0.19; 'import': 0.21; 'cc:2**0': 0.23; 'work.': 0.23; 'statement': 0.23; 'seems': 0.23; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'skip:" 20': 0.26; 'guess': 0.27; 'used,': 0.27; 'module.': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'probably': 0.29; 'file': 0.32; 'instead,': 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; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'copying': 0.38; 'files': 0.38; 'object': 0.38; 'skip:" 10': 0.40; 'think': 0.40; 'your': 0.60; 'first': 0.61; 'natural': 0.65; 'results': 0.65; '2013': 0.84; 'isaac': 0.84; 'oscar': 0.84; 'subject:Import': 0.84; 'affected.': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=+Y7syApGdaQDCXT1Tho7KQO0DWkz2oCuHiXVkwlOaYg=; b=SkvItgTTTXMVL90Q4szTvRdRmEWVqiWfl/tGwgqJQAvAbUPZMhaqz/EN3BBdoWHHCG zgsKZD+1Svf7muXYuzByS+DBMkPV8amUg0q3wnL9NJL7JJlF01KDL4PAExa5dKy5TOpp 45jzy7DtLDuPincwMhIHkVWwlLfkrfZ+kJdO+RqDbaXJilQK3QExMlm6nkFD800Me1hq wYtV3hmIUHqk3m8R9O2amKqs6ah6bZydQQk+3mL77FoI+Otc9N+yGQ8mJWbBBp/TLowb lRGZBqFJmEugEVDGcFg3nl3AlXZoXwoT+nzU1wYyNhgYo0h8WtWVYqRsouAu5A5TtrMf BpLw== |
| X-Received | by 10.152.110.116 with SMTP id hz20mr3846860lab.18.1360585690219; Mon, 11 Feb 2013 04:28:10 -0800 (PST) |
| MIME-Version | 1.0 |
| In-Reply-To | <CAOArY3VQFytYUZrKxxRHOYQfxP85=P6yT5b83wzpJBMwK4hKGQ@mail.gmail.com> |
| References | <CAOArY3VQFytYUZrKxxRHOYQfxP85=P6yT5b83wzpJBMwK4hKGQ@mail.gmail.com> |
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Date | Mon, 11 Feb 2013 12:27:50 +0000 |
| Subject | Re: Import redirects |
| To | Isaac To <isaac.to@gmail.com> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Cc | Python List <python-list@python.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| 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.1639.1360585692.2939.python-list@python.org> (permalink) |
| Lines | 41 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1360585692 news.xs4all.nl 6878 [2001:888:2000:d::a6]:54884 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:38668 |
Show key headers only | View raw
On 11 February 2013 06:50, Isaac To <isaac.to@gmail.com> wrote: > 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. A symlink would not have these problems, although it's not a completely portable solution. > 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. 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 Oscar
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Import redirects Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-11 12:27 +0000
csiph-web