Relative imports in Python 3.0

Benjamin musiccomposition at gmail.com
Wed Dec 17 13:48:05 EST 2008


On Dec 17, 4:01 am, Nicholas <nicholas.c... at gmail.com> wrote:
> 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?

Use absolute imports:

from ModuleDir import a

>
> Best wishes,
>
> Nicholas




More information about the Python-list mailing list