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


Groups > comp.lang.python > #108441

Are imports supposed to be like this?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Brendan Abel <007brendan@gmail.com>
Newsgroups comp.lang.python
Subject Are imports supposed to be like this?
Date Mon, 9 May 2016 16:54:00 -0700
Lines 39
Message-ID <mailman.552.1462838834.32212.python-list@python.org> (permalink)
References <CAPG_q=pkpjRTU3vRbLR-MCcPiaJxLO4fyoYZTHczvVTtjbMDtg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 6a6o3uNgqtixZZHfxbMq/g/atK/PQCDv5zUC+8if9rKA==
Return-Path <007brendan@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python3': 0.05; 'ugly': 0.07; 'imports': 0.09; 'bug': 0.10; 'python': 0.10; 'package,': 0.13; 'syntax': 0.13; '__init__.py': 0.16; 'a.py': 0.16; 'b.py': 0.16; 'only)': 0.16; 'other:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'alternate': 0.18; 'circular': 0.18; 'nested': 0.18; 'odd': 0.18; 'skip:` 10': 0.18; 'fix': 0.21; 'work,': 0.21; '(the': 0.22; 'explicit': 0.22; 'implicit': 0.22; 'seems': 0.23; 'absolute': 0.23; "haven't": 0.24; 'import': 0.24; 'long,': 0.24; 'module': 0.25; 'example': 0.26; 'rest': 0.26; 'possibility': 0.27; 'least': 0.27; 'message-id:@mail.gmail.com': 0.27; 'raise': 0.29; 'relative': 0.30; "i'd": 0.31; 'skip:_ 10': 0.32; 'generally': 0.32; 'subject:like': 0.33; 'throughout': 0.34; 'this?': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'skip:& 10': 0.37; 'received:209': 0.38; 'several': 0.38; 'received:209.85.220': 0.38; 'does': 0.39; 'rather': 0.39; 'skip:x 10': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'skip:u 10': 0.61; 'entire': 0.61; 'different': 0.63; 'deeply': 0.64; '4th': 0.67; '1st': 0.83; 'subject:this': 0.85; 'reasoning': 0.91; 'subject:Are': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to; bh=pVuTBnLGZBzDDriNr5nwKawIKmU+Amslx7Bh1yQCfGY=; b=nTnnUPoSYaLaMNc99yPc+ClSmMDBzjv+Ws9u+HLcef3bkwbFoJZ5Ttfz7DmrsoWaw8 OLCosvWw2R53a6KlcP1tvSsG/SzCmP1zjuKPmkroj45pxbwqHfxYKlmrWuE2O7DkIVP5 vwnLehLOV9D4Q5i9IP+05DjAJs78BxHkleMNDpVWBqpNlTb984bGiNUnO5d/PWJ1ra/F IVb8VFUj6HCPfA34ujl4UzvytHy9+PojpQ/C3GBF4Ud5aGOzhaS1jCpouKQRY8vrzeWH 8z9QLIEujY1MhCoJe/RRNtDJzCec754XjQPDhAPVJCfIL7Z91j2Ga+ofhGodAaEMKZDO cIiQ==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to; bh=pVuTBnLGZBzDDriNr5nwKawIKmU+Amslx7Bh1yQCfGY=; b=lcIlHA9/LBA4btwhlK4ds+FHXBE1sJMn3QwloAetcp6VXhqzruMVw/9oWZ5zXFxOZD VtGBe71cWrDSWwKGm4QoiNbtNufrbOMA4hkTuivSbtp0Q/JoBRO6QT7PPtJSCQW69E5q zxzimu21F2QH+TS/R2b1T+WwQF/684Bt047tRIxwJ3fHo74UnkHNmq2UANNdw4Co85dm ZGZDfvc11T4nfSVNiH9VzjbXh9ceZUkoavz7PS/w7p3zC/balZo8l4j9fwG5EDQuriM4 jhQoGqW9sfPHboWKWNHeZkQEm9txe5yEZIziRDQ+kYLu5mje11fF3bCNUvvbsQYX+T5G r35A==
X-Gm-Message-State AOPr4FU+SjQ0GKyGl/F4ekm8Aw8ViuyaEN2Glm9kWkp9O8FSok5e/ZTctw0eOmqFzifEtAr1mVSfbLcLD6Xp3A==
X-Received by 10.55.109.7 with SMTP id i7mr41164070qkc.127.1462838040245; Mon, 09 May 2016 16:54:00 -0700 (PDT)
X-Mailman-Approved-At Mon, 09 May 2016 20:07:13 -0400
X-Content-Filtered-By Mailman/MimeDel 2.1.22
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 <CAPG_q=pkpjRTU3vRbLR-MCcPiaJxLO4fyoYZTHczvVTtjbMDtg@mail.gmail.com>
Xref csiph.com comp.lang.python:108441

Show key headers only | View raw


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.

I'd much rather use relative imports, or the "from x import y" syntax, or
at least be able to use the "as" import syntax.  If I have a deeply nested
package, the imports become unruly rather quickly, and I have to use that
long, ugly name throughout the entire module!

    import package.subpackage.submodule.module  # fugly!

Are imports designed to work this way, or is this a bug in the import
machinery?  What reasoning is there for the first syntax to work, but all
the others should fail?  Admittedly, I haven't used Python3 yet, does it
fix this?  It seems odd to me that the documentation seems to encourage
relative imports or at least the "from x.y.z import a" forms of imports,
yet they don't work the same as "import x.y.z.a".


//Brendan

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


Thread

Are imports supposed to be like this? Brendan Abel <007brendan@gmail.com> - 2016-05-09 16:54 -0700

csiph-web