Are imports supposed to be like this?

Random832 random832 at fastmail.com
Mon May 9 20:26:05 EDT 2016


On Mon, May 9, 2016, at 19:54, Brendan Abel wrote:
> Consider the following example python package where `a.py` and `b.py`
> depend on each other:
> 
>     /package
>         __init__.py
>         a.py
>         b.py
> 
> 
> There are several ways I could import the "a.py" module in "b.py"
> 
>     import package.a           # Absolute import
>     import package.a as a_mod  # Absolute import bound to different name
>     from package import a      # Alternate absolute import
>     import a                   # Implicit relative import (deprecated,
>     py2
> only)
>     from . import a            # Explicit relative import

Can you show a complete example of what doesn't work with one or more of
these?

Because if I have:
- empty package/__init__.py
- package/a.py consisting of only "from . import b"
- package/b.py consisting of only "from . import a"
- in the directory containing 'package', running python 3.5
interactively, executing:
>>> import package.a

everything seems to work.



More information about the Python-list mailing list