Absolute imports in Python

In Python 2.4 and earlier, type the whole package and subpackage hierarchy when you import from a package (for example, from graphics.filters import noise). If you don't, someone reading your import statement has no way of knowing whether it imports another module from the package or a top-level package that's part of the Python distribution.

TECHNICAL Although it's possible to use relative imports in Python 2.4 and STUFF earlier, the rules are too complicated to document in this book.

Absolute importing, new in Python 2.5, addresses this problem. There's no ambiguity about which module is being imported because Python searches for imported modules or packages on sys.path only. To import sibling modules in a package, use relative imports (see the following section). To access absolute importing, type this code into your module or into interactive mode:

from future import absolute import

Was this article helpful?

0 0

Responses

  • richard
    What is absolute imports in python?
    2 years ago

Post a comment