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


Groups > comp.lang.python > #108442

Re: Are imports supposed to be like this?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Are imports supposed to be like this?
Date Tue, 10 May 2016 10:20:33 +1000
Lines 75
Message-ID <mailman.553.1462839636.32212.python-list@python.org> (permalink)
References <CAPG_q=pkpjRTU3vRbLR-MCcPiaJxLO4fyoYZTHczvVTtjbMDtg@mail.gmail.com> <CAPTjJmrorOJ-At=_uhcegobB5AfV9WiKL-jTz=FNqB_3PNMRxA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de fYrzQVlRX0gDj42nxvUIWAcZUlYqZVEhAErcYtZ/e3gg==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'failing': 0.05; 'python3': 0.05; 'dependency': 0.07; 'cc:addr:python-list': 0.09; 'forcing': 0.09; 'imports': 0.09; 'mkdir': 0.09; 'optional': 0.09; 'python': 0.10; 'syntax': 0.13; 'def': 0.13; '2016': 0.16; '__init__.py': 0.16; 'a.py': 0.16; 'b.py': 0.16; 'fail,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'only)': 0.16; 'other:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:> 20': 0.16; 'tried:': 0.16; 'wrote:': 0.16; 'alternate': 0.18; 'circular': 0.18; 'skip:` 10': 0.18; '>>>': 0.20; 'load': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'prevent': 0.20; '(the': 0.22; 'explicit': 0.22; 'implicit': 0.22; 'latter': 0.22; 'am,': 0.23; 'absolute': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'example': 0.26; 'linux': 0.26; 'rest': 0.26; 'possibility': 0.27; 'message-id:@mail.gmail.com': 0.27; 'skip:( 20': 0.28; 'fine': 0.28; 'cat': 0.29; 'raise': 0.29; 'relative': 0.30; 'generally': 0.32; 'though,': 0.32; 'subject:like': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'skip:> 10': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'setting': 0.37; 'received:209': 0.38; 'several': 0.38; 'does': 0.39; 'takes': 0.39; 'where': 0.40; 'some': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'more': 0.63; 'different': 0.63; 'strictly': 0.64; 'touch': 0.66; '4th': 0.67; '1st': 0.83; '5.3.1': 0.84; 'chrisa': 0.84; 'load),': 0.84; 'order:': 0.84; 'subject:this': 0.85; 'to:none': 0.91; 'subject:Are': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=hi3oQvJC6rJzLfhzvTQ6mnJUNTZH3dKyKp2FxVoMs6s=; b=IuaTlxueRFeArJB4fXr6QnQy/tSQ4/Egtt5Je9nLHHR6vVDsGDJQTRkzs58EJ6a2St IEjuaCcBlEU0OUqvPW5l2NTd25/HVK/U5Ou2BvLi/6Sl9/slreeL37nZzyzVji8OwG0C pYKw9lHNlf7IW+KHqpFQ2keUFyPfwSXKsSk7tUkENPLnEYb2sWc1TV57BcRwxdrfbw0y Iwlr0r5LR2S1Mh6erkFaeN3bqD4ICGIZ9QayawyN+1qgifJRF2g9CyBI6f6TdD0DsFoy 1EVQAUe56Y48LcsIvbeEXlV5SQ/6s9ED9YdN4Sozc2UFDaVkGlykSRkVWty3ZxOz4P8Q EiCA==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=hi3oQvJC6rJzLfhzvTQ6mnJUNTZH3dKyKp2FxVoMs6s=; b=d1OrhGyCm+1KdY3e2JzzrzMTcobVGs3sHlzvyMKyJLzGaRubi8MhtSjy2qtmL1KREF jGUZR5m/HrTlvcqOiA0iPv223JOEasnhLhBGswpFbbgab1T2otpnZrGkpw6hf7gkkv2W T850AXQRTjPOOxQQqoQImCllA7sl50ARBC5AEhyNfcPbkrag02fxC3i7tcR4RRExMAEm x4jgqdjhfrKTNfWsN8lxiNoE7IcyRnapDjOIu0jyk1Aoralxo/ge9iAM0U/YsPHpfEE3 gTU5sc5n/XE3byU4c2uDJzVWvNO7JiAfKg6th3SICmqmhoPXsoMTtkB4MiuCeV0BB1Ka XIUA==
X-Gm-Message-State AOPr4FUcoNX5/NEr6MeEHdRUWqWrHdOmljmY6WU91nDMWGBwnb7xsxyOwFxUwGjYcmVmRq9qFRU4ayIp/YlBPA==
X-Received by 10.112.198.72 with SMTP id ja8mr15577062lbc.112.1462839633958; Mon, 09 May 2016 17:20:33 -0700 (PDT)
In-Reply-To <CAPG_q=pkpjRTU3vRbLR-MCcPiaJxLO4fyoYZTHczvVTtjbMDtg@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CAPTjJmrorOJ-At=_uhcegobB5AfV9WiKL-jTz=FNqB_3PNMRxA@mail.gmail.com>
X-Mailman-Original-References <CAPG_q=pkpjRTU3vRbLR-MCcPiaJxLO4fyoYZTHczvVTtjbMDtg@mail.gmail.com>
Xref csiph.com comp.lang.python:108442

Show key headers only | View raw


On Tue, May 10, 2016 at 9:54 AM, Brendan Abel <007brendan@gmail.com> 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
>
> Unfortunately, only the 1st and 4th syntax actually work when you have
> circular dependencies (the rest all raise `ImportError` or
> `AttributeError`), and the 4th syntax only works in python 2 and is
> generally discouraged because of the possibility of name conflicts.

The fifth is the one that I would recommend. Can you give an example
of a circular dependency that makes it fail? Here's the trivial case
that I tried:

rosuav@sikorsky:~/tmp$ mkdir package
rosuav@sikorsky:~/tmp$ touch package/__init__.py
rosuav@sikorsky:~/tmp$ cat >package/a.py
from . import b
def func_a(x):
    print("func_a: %d" % x)
    if x % 2: b.func_b(x-1)
rosuav@sikorsky:~/tmp$ cat >package/b.py
from . import a
def func_b(x):
    print("func_b: %d" % x)
    if x % 2: a.func_a(x-1)
rosuav@sikorsky:~/tmp$ python3
Python 3.6.0a0 (default:98678738b7e9, May  2 2016, 13:37:04)
[GCC 5.3.1 20160409] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import package.a
>>> package.a.func_a(5)
func_a: 5
func_b: 4
>>> import package.b
>>> package.b.func_b(5)
func_b: 5
func_a: 4
>>>

The imports work fine as long as you use strictly "from . import
modulename", and not "from .modulename import objectname". Circular
imports of the latter form will indeed fail, but if you have
non-circular imports, you can stop them from failing by forcing module
load order:

rosuav@sikorsky:~/tmp$ cat package/a.py
from .b import func_b
def func_a(x):
    print("func_a: %d" % x)
    if x % 2: func_b(x-1)
rosuav@sikorsky:~/tmp$ cat >package/__init__.py
from . import a
from . import b


This does prevent lazy loading, though, so if your circular imports
are in an optional part of the package (and one that takes a long time
to load), you might want to consider setting it up some other way.

ChrisA

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


Thread

Re: Are imports supposed to be like this? Chris Angelico <rosuav@gmail.com> - 2016-05-10 10:20 +1000

csiph-web