Moving code from __init__.py but maintaining backward compatibility

I am working on a Python project where a previous developer placed most of the code in a base file __init__.py. I would like to be able to move the code from a file to a new file in a subdirectory.

spam/
    __init__.py
    foobar/
        __init__.py
        eggs.py

Thus, importing a spam module will use the code in foobar / eggs.py.

I would like to keep compatibility at 100%, because code that implements spam cannot be changed.

+5
source share
3 answers

100% compatibility is probably not possible. A few things (like pickle) really care about where something is defined.

, / __init__.py.

+3

from foobar import * /__ init__.py , , . , , foo = foobar.newfoo, .

, , , , , .

0

__init__.py foobar.egg , , , .

from foobar.eggs import apples_newname as apples_oldname
# or (but import * isn't recommended except for extreme cases)
from foobar.eggs import *

- __init__.py,

import foobar.eggs
def apples_newname(*args, **kwargs):
    foobar.eggs.apples_oldname(*args, **kwargs)
0

All Articles