Convert implicit relative imports to absolute imports

Implicit relative imports do not work in Python 3, so these have been
converted to absolute imports.  However, doing this causes circular
imports to fail (even in Python 2), so some code was moved around
to avoid this problem.