Relative imports in Python 3.0

Nicholas nicholas.cole at gmail.com
Wed Dec 17 05:01:17 EST 2008


Imagine a module that looks like

ModuleDir
     __init__.py
     a.py
     b.py


In python 2.x I used to have tests at the end of each of my modules,
so that module b.py might look something like

import a
 ..........
 ..........

if __name__ == '__main__':
   runtests()

But under Python 3.0 this seems impossible.  For usual use import a.py
has to become the line:

from . import a

But if I use that form it is no longer possible to run b.py as a
standalone script without raising an error about using relative
imports.

I am sure I am not the first to run into this issue, but what is the
solution?

Best wishes,

Nicholas



More information about the Python-list mailing list