#5123 [py3] Use absolute imports
Closed: Fixed None Opened 8 years ago by cheimes.

Python 3 no longer supports implicit relative imports. All imports are either absolute, fully qualified imports or explicit relative imports. See https://www.python.org/dev/peps/pep-0328/

Example:

ipapython/
   __init__.py
   errors.py
   ipautils.py

In Python 2 the ipautils module can import the errors module in three different ways:

# implicit relative import
import errors

# absolute import
from ipapython import errors

# explicit relative import
from . import errors

In Python 3 the first import would fail. Module dotted path are only resolved from directories in sys.path. In order to get the same behavior in Python 2 all imports must be made fully-qualified imports and every module must have a {{{from future import absolute_imports}}} line. NOTE: Future imports must be placed before all other imports.

risks

With relative looks it's no longer possible to run a module as a script. For example {{{python ipapython/ipautils.py}}} will no longer work. You can use {{{python -m ipapython.ipautils}}} instead.

migration

The modernize tool can handle both tasks with no extra work. The current release 0.4.0 has a small bug: https://github.com/python-modernize/python-modernize/issues/18. The bug is already fixed in git.


In the last 12 months all Python files have been migrated to absolute imports.

Metadata Update from @cheimes:
- Issue assigned to someone
- Issue set to the milestone: FreeIPA Python 3 support

7 years ago

Login to comment on this ticket.

Metadata