Absolute and Explicit Relative Python Module Import

I am wondering how to choose import packages in a Python application. I have a package structure like this:

project.app1.models project.app1.views project.app2.models 

project.app1.views imports project.app1.models and project.app2.models . There are two ways to do this that come to mind.

With absolute import:

 import AA import ABB 

or with explicit relative imports introduced in Python 2.5 with PEP 328 :

 # explicit relative import ..A import .B 

What is the most pythonic way to do this?

+56
python package python-import
Nov 17 '10 at 22:07
source share
3 answers

Absolute import. From PEP 8:

Relative imports for in-package imports are not recommended. Always use the absolute package path for all imports. Even now that PEP 328 [7] is fully implemented in Python 2.5, its explicit relative import style is actively discouraged; absolute imports are more portable and usually more readable.

Explicit relative imports are a good feature of the language (I think), but they are not as pronounced as absolute imports. More readable form:

 import AA import ABB 

especially if you import several different namespaces. If you look at some well-written projects / tutorials containing import from packages, they usually follow this style.

A few extra keystrokes that you take to be more explicit may save others (and maybe you) a lot of time in the future when they try to figure out your namespace (especially if you switch to 3.x, in which some of package names have changed).

+28
Nov 17 '10 at 22:22
source share

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
+89
May 25 '13 at 9:54
source share

Relative imports not only allow you to freely rename your package later without changing dozens of domestic imported goods, but I have also been successful with them in resolving certain issues related to things like round-robin import or namespace packages because they don't send Python " back to top "to start searching again for the next module from the top-level namespace.

+27
Nov 17 '10 at 22:25
source share



All Articles