I just stumbled across this unexpected behavior in python (both 2.7 and 3.x):
>>> import re as regexp
>>> regexp
<module 're' from '.../re.py'>
>>> from regexp import search
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'regexp'
Of course from re import search succeeds, just as it would have before I created the alias. But why can't I use the alias regexp, which is now a known module, as a source for importing names?
This sets you up for a nasty surprise whenever there are multiple variants of a module: Say I am still using Python 2 and I want to use the C version of pickle, cPickle. If I then try to import a name from pickle, it will be fetched from the simple pickle module (and I won't notice since it doesn't throw an error!)
>>> import cPickle as pickle
>>> from pickle import dump
>>> import inspect
>>> inspect.getsourcefile(dump)
'.../python2.7/pickle.py' # Expected cPickle.dump
Oops!
Poking around I see that sys.modules includes the real module name (re or cPickle, but not the alias regexp or pickle. That explains how the second import fails, but not why python module name resolution works this way, i.e. what the rules and rationale are for doing it this way.
Note: This was marked as a duplicate of a question that has nothing to do with module aliasing: aliasing is not even mentioned in the question (which is about importing submodules from a package) or the top answers. While the answers to that question provide information relevant to this question, the questions themselves are not even similar IMHO.