19

I'm changing a bunch of old python code that is occasionally running into name collisions between packages. I have a question about when absolute imports should be used and whether it would be proper to import same-level modules by name only.

/package/
/package/__init__.py
/package/subA
/package/subA/__init__.py
/package/subA/moduleA.py
/package/subA/moduleB.py
/package/subB
/package/subB/__init__.py
/package/subB/moduleA.py
/package/subB/moduleB.py

Should every import statement within the package look like:

import package.subX.moduleX

or

from package.subX import moduleX

What about in the subpackage __init__.py files. Would it be wrong to simply put

import moduleA
import moduleB

Or, in /package/subA/moduleA.py, would it be wrong to simply put:

import moduleB
  • you could always alias as well if you want to save some characters throughout your code, as in `from package.subA import moduleA as a_moduleA` and `from package.subB import moduleA as b_moduleA`, seems kind of messy to me, but it's another option... – photoionized Apr 27 '11 at 22:51
  • possible duplicate of [Absolute vs. explicit relative import of Python module](http://stackoverflow.com/questions/4209641/absolute-vs-explicit-relative-import-of-python-module) – Stefano Sep 22 '15 at 16:01

1 Answers1

25

Relative imports turned out to be a very bad idea, even though they were the default behavior for long. You can find quite a few questions on this site where someone simply named their file after a builtin module and broke their application with weird error messages.

That's why it's always a good to do absolute imports by referencing your project everywhere, including packages.

In short, use this style:

import myproject.mypackage
from myproject.mypackage.myfile import MyClass

Quote from PEP8:

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

Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
  • 3
    +1 with absolute imports it's easy to see what you're going to get. – samplebias Apr 28 '11 at 00:02
  • 23
    As of September 213, this answer is wrong because PEP8 has changed. For example: "However, explicit relative imports are an acceptable alternative to absolute imports, especially when dealing with complex package layouts where using absolute imports would be unnecessarily verbose" – glarrain Sep 16 '13 at 16:56
  • @glarrain But absolute imports are still recommended over relative – endolith May 29 '22 at 18:01
  • @endolith recommended by whom? You mean as popular convention, a PEP or what? – glarrain Aug 11 '22 at 13:19
  • @glarrain Yes: "Absolute imports are recommended, as they are usually more readable and tend to be better behaved … explicit relative imports are an acceptable alternative to absolute imports" https://peps.python.org/pep-0008/#imports "Absolute imports are preferred because they are quite clear and straightforward" https://realpython.com/absolute-vs-relative-python-imports/ – endolith Aug 11 '22 at 14:36