The difference between absolute and relative , which is drawn here, is very similar to the way we talk about absolute and relative file paths or even URLs.
The absolute {import, path, URL} tells you exactly how to get what you need, usually specifying each part:
import os, sys from datetime import datetime from my_package.module import some_function
Relative (import, paths, URLs) is exactly what they say: they are relative to their current location. That is, if the directory structure changes or the file moves, it may break (because they no longer mean the same thing).
from .module_in_same_dir import some_function from ..module_in_parent_dir import other_function
Therefore, absolute imports are preferable for code to be shared.
In the comments, I was asked to give an example of how from __future__ import absolute_import communicates with this and how it is intended to be used. Trying to formulate this example, I realized that I also could not explain its behavior, so I asked a new question . This answer gives a sample code showing the correct working implementation from __future__ import absolute_import , where it actually removes the ambiguity.
The accepted answer describes in more detail why this works the way it happens, including a discussion of the confusing wording of the Python 2.5 change log. In essence, the scope of this directive (as well as expanding the distinction between absolute and relative imports in Python) is very, very narrow. If you need to find these differences in order to get your code to work, you are probably better off not renaming your local module, if at all possible.
Two-Bit Alchemist Mar 27 '14 at 6:07 2014-03-27 06:07
source share