I have the following structure for my python library:
a\
-- b\
-- __init__.py
-- foo.py
My __init__.py
contains the following code:
from a.b.foo import Foo
So users of this library can use the following import statements:
from a.b import Foo
from a.b.foo import Foo
I wish to move the file foo.py
to a new package as such:
a\
-- x\
-- __init__.py
-- foo.py
allowing the users to do the following:
from a.x import Foo
from a.x.foo import Foo
while still retaining the ability for the existing users to do
from a.b import Foo
from a.b.foo import Foo
The approaches I have so far are:
- Retain the
a\b\foo.py
but replace the contents to refer to the new location. Essentially the contents ofa\b\foo.py
would be:
from a.x.foo import FooImported
Foo = FooImported
- Mess with sys.modules. This has been known to produce subtle issues which makes me hesitant to go this route.
Is there an alternative?
A solution where I could remove a\b\foo.py
and update a\b\__init__.py
to setup aliases for the old package path to reference the new one would be nice, but I haven't been able to figure out how to do that.