Should I use "from import packages, settings" or "from. Import utils, settings",

I am developing a Python application; it has all its code in one package and, of course, passes inside it. The Python application package is not of interest to the user, it is just a GUI application.

The question is which style is preferable when importing modules inside the application package.

from application import settings, utils 

or

 from . import settings, utils 

That is, I can either specify the name as it is (here is the "application"), or I can say "current package" using "."

This is a free software package, so there is a possibility that someone wants to plug in my application and change its name. In this case, alternative 1 is a little nuisance. However, I use style 1 all the time (although early code uses style 2 in some places) since style 1 looks a lot better.

Are there any arguments for my style (1) that I missed? Or is it stupid not to go with style 2?

+4
source share
1 answer

The Python style guide recommends explicitly against relative imports (style.):

Relative imports for intra-batch imports are highly discouraged. 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.

I tend to agree. Relative imports mean that the same module is imported differently in different files and requires me to remember what I look at when reading and writing. Not worth it, and renaming can be done using sed .

Besides the renaming problem, the only problem with absolute imports is that import foo can mean a top-level module foo or a submodule foo under the current module. If this is a problem, you can use from __future__ import absolute_import ; this is standard in Python 3.

+10
source

All Articles