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.
__init__.py
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.
100% compatibility is probably not possible. A few things (like pickle) really care about where something is defined.
, / __init__.py.
from foobar import * /__ init__.py , , . , , foo = foobar.newfoo, .
from foobar import *
foo = foobar.newfoo
, , , , , .
__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)