Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108443

Re: Are imports supposed to be like this?

From Random832 <random832@fastmail.com>
Newsgroups comp.lang.python
Subject Re: Are imports supposed to be like this?
Date 2016-05-09 20:26 -0400
Message-ID <mailman.554.1462839968.32212.python-list@python.org> (permalink)
References <CAPG_q=pkpjRTU3vRbLR-MCcPiaJxLO4fyoYZTHczvVTtjbMDtg@mail.gmail.com> <1462839965.2919682.602944817.37131FE3@webmail.messagingengine.com>

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Are imports supposed to be like this? Random832 <random832@fastmail.com> - 2016-05-09 20:26 -0400

csiph-web