0

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:

  1. Retain the a\b\foo.py but replace the contents to refer to the new location. Essentially the contents of a\b\foo.py would be:
from a.x.foo import FooImported

Foo = FooImported
  1. 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.

user1925405
  • 332
  • 1
  • 5
  • 13
  • Does this answer your question? [Can I "fake" a package (or at least a module) in python for testing purposes?](https://stackoverflow.com/questions/5122465/can-i-fake-a-package-or-at-least-a-module-in-python-for-testing-purposes) – Tim Oct 03 '22 at 03:01

0 Answers0