Search code examples
pythonpackagepython-importbackwards-compatibility

Backwards compatibility: Moving a module into a subpackage


Lets say I have a Python package with this structure:

project
 +---> module1.py
 +---> module2.py
 +---> module3.py

Now, I decide to group these into subpackages:

project
 +---> topic1
   +---> module1.py
   +---> module2.py
 +---> topic2
   +---> module3.py

Let's further assume there are a lot of users in other projects (that I don't want to update/email/break their code without knowing) that use the following:

from project.module1 import AwesomeClass

This wouldn't work with the new design anymore.

What I can do is:

project
 +---> module1.py
 +---> topic1
   +---> module1.py
   +---> module2.py
 +---> topic2
   +---> module3.py

and the file project/module1.py contains only one line

# For backwards compatability
from project.topic1.module1 import AwesomeClass

This is not very pretty though and is kind of the opposite of refactoring.

Is there something that you could put in __init__.py to make it work, or some other hack to link it to it's old place without being so visible?


Solution

  • I think

    # For backwards compatability
    from project.topic1.module1 import AwesomeClass
    

    is the only sensible solution.