Warm tip: This article is reproduced from stackoverflow.com, please click
python python-2.7 import python-importlib

Python how to alias module name (rename with preserving backward compatibility)

发布于 2020-03-27 10:23:18

I have a python package named foo, which i use in imports:

import foo.conf
from foo.core import Something

Now i need to rename the foo module into something else, let's say bar, so i want to do:

import bar.conf
from bar.core import Something

but i want to maintain backward compatibility with existing code, so the old (foo.) imports should work as well and do the same as the bar. imports.

How can this be accomplished in python 2.7?

Questioner
Mariusz Jamro
Viewed
152
ThinkChaos 2014-06-20 17:55

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)