Relative Python imports are no longer very discouraged, but absolute_import is strongly recommended in this case.
Please see this discussion , citing Guido himself:
"Isn't this mostly historical? Prior to the new relative import syntax, various problems with relative imports were implemented. The short-term solution was to recommend not using them. The long-term solution was to implement an unambiguous syntax. Now it's time to remove the anti-recommendation. Of course, not going overboard - I still find them acquired taste; but they have their place. "
The OP correctly binds PEP 328 , which states:
Several use cases were presented, the most important of which is the ability to rebuild the structure of large packages without the need to edit subpackages. In addition, the module inside the package cannot easily import itself without relative import.
Also see the almost duplicated question When or why to use relative imports in Python
Of course, it still tastes good. While it is easier to move code with relative imports, it can also break things unexpectedly; and renaming imports is not that difficult.
To force the new behavior from PEP 328 to use:
from __future__ import absolute_import
In this case, implicit relative imports are no longer possible (for example, import localfile will no longer work, only from . import localfile ). For clean and future behavior, absolute_import is recommended.
An important caveat is that because of PEP 338 and PEP 366 , relative imports require the python file to be imported as a module - you cannot execute a .py file that has relative imports, or you get ValueError: Attempted relative import in non-package .
This limitation should be considered when evaluating the best approach. Guido is in any case against running scripts from the module:
I am on this and on any other __main__ tweedles offered. The only use case seems to be to run scripts that happen to live inside the module directory, which I have always considered antipattern. To make me change my mind, you have to convince me that it is not.
Detailed discussions on this subject can be found on SO; Python 3 Reynolds number is pretty complete:
- Relative import in Python 3