I'm trying to support multiple versions of a python package without impacting client code.
Consider the following repo:
.
|-- client_code.py
`-- lib
|-- __init__.py
`-- foo.py
client_code.py:
from lib.foo import f
...
f()
I'd like to leave client_code.py unchanged. I first tried to do something like this:
lib
|-- __init__.py
|-- v1
| |-- __init__.py
| `-- foo.py
`-- v2
|-- __init__.py
`-- foo.py
lib/__init__.py:
import os
if os.environ.get("USE_V2", "0") == "0": # Or some other runtime check
from .v1 import foo
else:
from .v2 import foo
But the client code fails with the following error:
Traceback (most recent call last):
File "client_code.py", line 1, in <module>
from lib.foo import f
ImportError: No module named foo
I'm aware that the following options would work, but they would require clients changing code:
if os.environ.get("USE_V2", "0") == "0":
from lib.v1.foo import f
else:
from lib.v2.foo import f
f()
if os.environ.get("USE_V2", "0") == "0":
import lib.v1.foo as foo
else:
import lib.v2.foo as foo
foo.f()
Is something like this possible?
A more general version of the question is here: Support two versions of a python package without clients needing to change code