Module absolute import in python
Python defaults to import module relatively. Here is description of python's module search order.
Give example:
$ mkdir -p /tmp/abs-import-test/{foo,bar}
$ cd /tmp/abs-import-test
$ touch {foo,bar}/__init__.py # make foo and bar as a python module
$ touch bar/boo.py # make bar.boo module
# make foo.bar module. it depends to bar.boo module created at above
$ echo "import bar.boo" > foo/bar.py
$ python
Python 2.7.5 (default, Oct 17 2013, 21:18:01)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo.bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "foo/bar.py", line 1, in <module>
import bar.boo
ImportError: No module named boo
>>>
Above script failed because python understands import bar.boo as "Import boo module which is relatively located under the bar module currently executed(foo/bar.py)" so python tries to load foo/bar/boo.py but it doesn't exists because I was expected to load bar/boo.py.
This can be solved by using absolute_import future.
$ echo -n "from __future__ import absolute_import\nimport bar.boo" > foo/bar.py $ python Python 2.7.5 (default, Oct 17 2013, 21:18:01) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import foo.bar >>> foo.bar.bar.boo.__file__ 'bar/boo.py'