0

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 relateive from . imports
dabljues
  • 1,663
  • 3
  • 14
  • 30
  • This sounds somewhat like "how do I set a debug flag for all modules and sub-modules?" to me. From there you could decide at each "\_\_init__" what to do in case of "debug" mode. See [this](https://stackoverflow.com/a/27455446/3220135) answer on how to do that. Also see the python [docs](https://docs.python.org/3/library/constants.html#__debug__) – Aaron Dec 11 '20 at 18:10
  • Hm, okay. I get the idea here, but the thing is, this example thing that I created is small. I would have to apply this `if` statement to one import and it's done. However, if I had to do this in my project, I would have to double the amount of imports from the package (which amount is like 50+, so it would be way over 100) and put them in `if/else` blocks. Don't know if that's the best solution here. It would be perfect if I could apply something like this only in my `__init__` file. But maybe that's what you meant and I just couldn't make it work – dabljues Dec 11 '20 at 18:51
  • Additionally, the `__debug__` flag wouldn't suffice here - I still would want to run my application (it's running in real time), make the changes in a certain amount of time and replace the old implementation with a new one with a single move – dabljues Dec 11 '20 at 18:55

0 Answers0