Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Import redirects

Reply
Thread Tools

Import redirects

 
 
Isaac To
Guest
Posts: n/a
 
      02-11-2013
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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Import redirects Isaac To Python 0 02-12-2013 02:50 AM
Re: Import redirects Oscar Benjamin Python 0 02-11-2013 12:27 PM
Re: Import redirects Chris Angelico Python 0 02-11-2013 07:32 AM
Re: Import redirects Mark Lawrence Python 0 02-11-2013 07:28 AM
'max-redirects' A Dickson Cisco 0 02-15-2006 04:43 PM



Advertisments