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


Groups > comp.lang.python > #106702

Re: Python programs and relative imports

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Python programs and relative imports
Date Sat, 9 Apr 2016 04:04:01 +1000
Lines 31
Message-ID <mailman.96.1460138650.2253.python-list@python.org> (permalink)
References <ndu70d$m0t$1@dont-email.me> <ne8o2e$van$1@dont-email.me> <CAPTjJmpNZkGE+eg_OnGYBw=0EDnGXq-KysE0qLiPNTp7NLATbA@mail.gmail.com> <mailman.94.1460135468.2253.python-list@python.org> <ne8r0n$b70$1@dont-email.me> <CAPTjJmoGEn7fDyf-918tFeATB2oQEV3MTKdHnsEAi5dmVAcoEQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de AlqTdle3QC0tOsRaTVKUdwHbj+8JAbnJc6ubzJehWLdA==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'subject:Python': 0.05; 'python3': 0.05; 'cc:addr:python- list': 0.09; 'semantics': 0.09; 'work"': 0.09; 'syntax': 0.13; 'importing': 0.15; '"current': 0.16; "'from": 0.16; '2016': 0.16; '__init__.py': 0.16; 'a.py': 0.16; 'ah,': 0.16; 'compatible.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'refactored': 0.16; 'say.': 0.16; 'wrote:': 0.16; 'of.': 0.18; '(in': 0.18; 'shell': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'fix': 0.21; 'context.': 0.22; 'am,': 0.23; 'sat,': 0.23; "haven't": 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; "i've": 0.25; 'message-id:@mail.gmail.com': 0.27; 'moved': 0.27; 'actual': 0.28; 'another.': 0.29; 'raise': 0.29; 'allows': 0.30; "i'm": 0.30; 'relative': 0.30; 'that,': 0.34; 'received:google.com': 0.35; 'on,': 0.35; 'could': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'things': 0.38; 'received:209': 0.38; 'files': 0.38; 'mean': 0.38; 'still': 0.40; 'here.': 0.62; 'packages,': 0.66; 'sit': 0.66; '"just': 0.84; 'chrisa': 0.84; 'to:none': 0.91; 'mean.': 0.91; 'spell': 0.91
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=7fpzaaVfDgT6tK+MvVZ5Ub6e16AfAvTgHVQUcs3SSXs=; b=Z8H91yhooahApwb7zHIT3HC7DNvEiCpGbivW9PK+mB9TZnD0W/g/z2kqtd7w+ejO8a w/m6yFIqq0y4wINV2UC6n0ezOtkWz/0MnMvhBEfRjZ58rddD92AHzmTbBrzwlYHIiGta /rqvYUTMFW3mCcJ26tbyZ9Rd/lR4CT9t4lzCzr3HTCxDLKGP/pegNGhXLrRp6r1dgDXJ 0Qp56Z3ixv+LZhzAQM5iSoDaqhavJuSg1Q4xznWudId7ncKbQlX2WExuFoHbWas0acnB Jj2FYnn4PiLKT59ahmsMByR2kd35eKTDXCv/q+w1sA5o7j8lntfkKTaRkXGtPLI6MiIz Nycg==
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=7fpzaaVfDgT6tK+MvVZ5Ub6e16AfAvTgHVQUcs3SSXs=; b=KL9jiO4YvZ6HCs7NwyIRuDQpq84F3CE6js73gOCWlHb/LebnPf41uArQXmhxkDmisE g+Bqp7f2+ApSv7n+cXTAExqSedBr26Woj0ukb43AfM35zZVgojV9tV8BSCpBAJdlEqzz 3F2J+kjNvJIXlGtolq0EsWkyE0puTyIGkxZwtK5VBv4Y+/mDBgpgA4jFdVF2cASSFknm mk5QHvqdfi7grKj2DZSKp+/uPDm7/nvQ/4bT19kavSzxgY4Ck+PgA5/B/OBGuII/Zidt SN6oAMi2RIRzK5falchDsY82pmyVlt9yYfPX+sJuUB6CMDJrFFvVyf5nbzk/PhcoEPoi RJzA==
X-Gm-Message-State AD7BkJJljANSBaSeJ7kEpI41AGx34VzDZVyTLLEtdJkgC4Kbgr4KTGs/jpErV5vm6fcgBmadI6JT/AUr2I7MYA==
X-Received by 10.107.18.147 with SMTP id 19mr10033330ios.157.1460138641586; Fri, 08 Apr 2016 11:04:01 -0700 (PDT)
In-Reply-To <ne8r0n$b70$1@dont-email.me>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
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 <CAPTjJmoGEn7fDyf-918tFeATB2oQEV3MTKdHnsEAi5dmVAcoEQ@mail.gmail.com>
X-Mailman-Original-References <ndu70d$m0t$1@dont-email.me> <ne8o2e$van$1@dont-email.me> <CAPTjJmpNZkGE+eg_OnGYBw=0EDnGXq-KysE0qLiPNTp7NLATbA@mail.gmail.com> <mailman.94.1460135468.2253.python-list@python.org> <ne8r0n$b70$1@dont-email.me>
Xref csiph.com comp.lang.python:106702

Show key headers only | View raw


On Sat, Apr 9, 2016 at 3:50 AM, Rob Gaddi
<rgaddi@highlandtechnology.invalid> wrote:
> Sort of.  If I've got a directory full of files (in a package)
> that I'm working on, the relative import semantics change based on
> whether I'm one directory up and importing the package or in the same
> directory and importing the files locally.  That is to say if I've got:
>
> pkg/
>   __init__.py
>   a.py
>   usedbya.py
>
> then there is no single syntax I can use in a.py that allows me to both
> sit in the pkg directory at the shell and poke at things and import pkg
> from the higher level.
>
> If the 'from . import usedbya' syntax were always available, then it
> would work the same in either context.  And if as I refactored things,
> as they moved in and out of packages, it would all still "just work" for
> files that haven't moved relative to one another.

Ah, I see what you mean. You're working inside an actual package here.
So you can "cd ..; python3 -m pkg.a", or you can "python3 a.py", but
not both.

The simplest fix for that would be to allow "python3 -m .a" to mean
"current directory is a package". I don't think there's currently a
way to spell that, but it ought to be completely backward compatible.
You could raise this on python-ideas and see what people say.

ChrisA

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


Thread

Python programs and relative imports Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-04 17:07 +0000
  Re: Python programs and relative imports Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-08 16:59 +0000
    Re: Python programs and relative imports Chris Angelico <rosuav@gmail.com> - 2016-04-09 03:11 +1000
      Re: Python programs and relative imports Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-08 17:50 +0000
        Re: Python programs and relative imports Chris Angelico <rosuav@gmail.com> - 2016-04-09 04:04 +1000
        Re: Python programs and relative imports Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-08 12:26 -0600

csiph-web