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


Groups > comp.lang.python > #106702

Re: Python programs and relative imports

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Python programs and relative imports
Date 2016-04-09 04:04 +1000
Message-ID <mailman.96.1460138650.2253.python-list@python.org> (permalink)
References (1 earlier) <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>

Show all headers | 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