Python how to alias module name (rename with preserving backward compatibility)
This forces you to keep a foo
directory, but I think it the best way to get this to work.
Directory setup:
bar
├── __init__.py
└── baz.py
foo
└── __init__.py
foo_bar.py
bar/__init__.py
is empty.bar/baz.py
: worked = True
foo/__init__.py
:
import sys
# make sure bar is in sys.modules
import bar
# link this module to bar
sys.modules[__name__] = sys.modules['bar']
# Or simply
sys.modules[__name__] = __import__('bar')
foo_bar.py
:
import foo.baz
assert(hasattr(foo, 'baz') and hasattr(foo.baz, 'worked'))
assert(foo.baz.worked)
import bar
assert(foo is bar)
This answer work with submodules:
import sys
import os
from importlib.abc import MetaPathFinder, Loader
import importlib
from MainModule.SubModule import *
class MyLoader(Loader):
def module_repr(self, module):
return repr(module)
def load_module(self, fullname):
old_name = fullname
names = fullname.split(".")
names[1] = "SubModule"
fullname = ".".join(names)
module = importlib.import_module(fullname)
sys.modules[old_name] = module
return module
class MyImport(MetaPathFinder):
def find_module(self, fullname, path=None):
names = fullname.split(".")
if len(names) >= 2 and names[0] == "Module" and names[1] == "LegacySubModule":
return MyLoader()
sys.meta_path.append(MyImport())
Do you mean something like this?
import foo as bar
you can use shortcuts for module imports like:
from numpy import array as arr
in: arr([1,2,3])
out: array([1, 2, 3])
and you can also use more than one alias at a time
from numpy import array as foo
in: foo([1,2,3])
out: array([1, 2, 3])
if your foo
is a class you can do:
bar=foo()
and call a subfunction of it by:
bar.conf()
Does this help you?