I want to achieve something odd I guess. Basically, I have a working implementation of my app. Now I want to change one of my app packages entirely. But I want at least for now leave the old implementation in place, to still use it in production.
I've created a sample project to reproduce the problem I have.
Let's say I have a dir structure like this:
│ setup.py
│
└───foo
│ __init__.py
│
└───foo1
│ foo1_module.py
│ __init__.py
│
└───foo2
foo2_module.py
__init__.py
What I want to do is to change the implementation of foo1
entirely. I want to apply the changes gradually and still have the old implementation being used.
So I moved the contents of foo/foo1
to foo/foo1/old
. Then I want to create a foo/foo1/new
directory and implement the changes in there:
│ setup.py
│
└───foo
│ __init__.py
│
└───foo1
│ __init__.py
│
└───old
│ foo1_module.py
│ __init__.py
│
└───foo2
foo2_module.py
__init__.py
The thing is, I don't want to change other packages' imports in my code.
So if an import looks like this: from foo.foo1.foo1_module import func
- I want it to stay like this, without having to change it to from from foo.foo1.old.foo1_module import func
.
Therefore, I put this in foo/foo1/__init__.py
:
from .old import *
Then I wanted to test it:
foo\foo1\old\foo1_module.py:
def func():
pass
foo\foo1\old\foo2\foo2_module.py:
from foo.foo1.foo1_module import func
File "e:\projects\python\imports\foo\foo1\old\foo2\foo2_module.py", line 1, in <module>
from foo.foo1.foo1_module import func
ModuleNotFoundError: No module named 'foo.foo1.foo1_module'
So the question is: can I somehow get the foo/foo1/__init__.py
to, I don't know, point to/import the things from the foo/foo1/old
, so for everyone importing this, the import paths would stay the same?
Cuz if not, I am left with two options:
- either add the
.old.
part in every absolute import - rearrange my package, so there are no parent package imports (like in
foo2_module.py
) and then use relateivefrom .
imports