Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Python programs and relative imports Date: Fri, 8 Apr 2016 12:26:52 -0600 Lines: 27 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de WMezJ6CWFBmjb78Oy0NLxw8yuF+VTSCC9LaWNy9VNgVg== Return-Path: 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; 'subject:Python': 0.05; 'semantics': 0.09; 'package,': 0.13; 'syntax': 0.13; 'importing': 0.15; "'from": 0.16; '2016': 0.16; '__init__.py': 0.16; 'a.py': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'of.': 0.18; 'tree': 0.18; '(in': 0.18; 'shell': 0.18; 'context.': 0.22; 'level,': 0.22; 'am,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'module': 0.25; "i've": 0.25; 'equivalent': 0.27; 'fri,': 0.27; 'packaging': 0.27; 'message-id:@mail.gmail.com': 0.27; 'module.': 0.27; 'allows': 0.30; "i'm": 0.30; 'relative': 0.30; 'file': 0.34; 'received:google.com': 0.35; 'on,': 0.35; 'could': 0.35; 'level': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'modules': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'received:209.85.213': 0.37; 'things': 0.38; 'received:209': 0.38; 'files': 0.38; 'to:addr:python.org': 0.40; 'some': 0.40; "you'll": 0.61; 'sit': 0.66; 'expect.': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=J8ZhjpkNOGjyV/Z60uvKKstGyisqMZ+D3S37KlKdoPE=; b=rul6MPE0EcAi6W1jQsqyIK0kejtwCaB5ehnRrFUlzvcRsEGdi4UKVh24UHIOAi7ptW G4In3B5FFHwcTpAy3ugOoXu1BMDblxsanUhW1k5eB+4sqRRgwwpKp9oiPxr+rnNaScVb D+CGq21N1z5d9+TlwpDbfbwmTJfAK096UjtIW28LZ2ADv6GQndFO0tlaGuHkoqXRwCfm AOONCGgRWR/qYCg8JQObRUnOob6aJeKukYElkWHN6dGQczjYCewGQIg05Z/GfNcrPcjt GLvKWVLER7mPrOzHexJgG+yqIKn7fsY35BDhqELIQiHM+HIXcr7k+doYzYMh+c+RiitU 4a4A== 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:from:date :message-id:subject:to; bh=J8ZhjpkNOGjyV/Z60uvKKstGyisqMZ+D3S37KlKdoPE=; b=m6B4sOiZd5ALHb8BUYcPNCxJN9vueMyul4mUd+2fo5fYNBDZuXPheQbGNDFsGeyWE1 N7AZNLs/UhtEc5c8LvlR/ZtA2ezUNKnfGlkf0lX6LFd6lBYKyy4q3d13wJECuxeYbipZ K89XeYBR5s5mI2BEjHtI4q3Dmoo9v9YNujp9ZJOPgCy6fvp76G/Mp+bAc51iwKZOxYQl QTnCFIZyx/oru8Z4CbizusIyNxrorMt5Ryl3OsaYbLZOa47lsNSK+kXEScVwtYSHvQxg crv2VnL/Hn0atJK1tMt+DkZwmMhAnu9tIa+foD9+hax57dTLb5TZ2hbBwBZUiGUIfGrW JWPg== X-Gm-Message-State: AD7BkJIrK6GHuyf2pZEfN6WZOyab2y/+JdeiOE8FYIkqJpizGxm9E2q7qIg8IB0BLEsYF/7t9vRrpnKY+2ImtQ== X-Received: by 10.50.57.50 with SMTP id f18mr5349893igq.93.1460140051540; Fri, 08 Apr 2016 11:27:31 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:106707 On Fri, Apr 8, 2016 at 11:50 AM, Rob Gaddi 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. Not necessarily. Inside the package, 'from . import usedbya' is effectively equivalent to 'import pkg.usedbya as usedbya'. Without the package, all of these modules are at the top level, and 'from . import usedbya' would conceptually be equivalent to 'import usedbya'. But there's no guarantee that the 'usedbya' module at the top level of the module tree is the same 'usedbya.py' file in the current directory; it could be shadowed by some other module. Whereas with the package, the packaging ensures that you'll get the module expect.