Re: Import redirects

Discussion in 'Python' started by Oscar Benjamin, Feb 11, 2013.

  1. On 11 February 2013 06:50, Isaac To <> 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
     
    Oscar Benjamin, Feb 11, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    0
    Views:
    315
  2. Isaac To

    Import redirects

    Isaac To, Feb 11, 2013, in forum: Python
    Replies:
    0
    Views:
    130
    Isaac To
    Feb 11, 2013
  3. Mark Lawrence

    Re: Import redirects

    Mark Lawrence, Feb 11, 2013, in forum: Python
    Replies:
    0
    Views:
    127
    Mark Lawrence
    Feb 11, 2013
  4. Chris Angelico

    Re: Import redirects

    Chris Angelico, Feb 11, 2013, in forum: Python
    Replies:
    0
    Views:
    125
    Chris Angelico
    Feb 11, 2013
  5. Isaac To

    Re: Import redirects

    Isaac To, Feb 12, 2013, in forum: Python
    Replies:
    0
    Views:
    102
    Isaac To
    Feb 12, 2013
Loading...

Share This Page