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


Groups > comp.lang.python > #109100

Re: Question about imports and packages

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: Question about imports and packages
Date Wed, 25 May 2016 14:39:56 +1000
Lines 48
Message-ID <mailman.77.1464151220.20402.python-list@python.org> (permalink)
References <CAPxRSnb=nEEjR63iNRJyepXo+aG6d+U7H2k5E_FbedC35tcsZQ@mail.gmail.com> <85iny2agyr.fsf@benfinney.id.au>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de sPvLIsd/WdFJ6U3yx2FJJAfXGa69ZuCLpyl1hDbUHVFg==
Cancel-Lock sha1:3oH+wQQAHJAGJHNN4mWVuXb4590=
Return-Path <python-python-list@m.gmane.org>
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; 'python3': 0.05; 'subject:Question': 0.05; 'deprecated': 0.07; 'imply': 0.07; 'means,': 0.07; 'filesystem': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'scripting': 0.09; 'python': 0.10; 'python.': 0.11; 'wed,': 0.15; '10:00': 0.16; '2016': 0.16; 'expects': 0.16; 'is::': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'directory.': 0.18; 'visible': 0.22; 'am,': 0.23; 'seems': 0.23; "python's": 0.23; 'import': 0.24; 'module': 0.25; 'script': 0.25; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'package.': 0.27; 'see,': 0.27; 'idea': 0.28; 'fine': 0.28; 'follows': 0.29; "i'm": 0.30; 'system,': 0.30; 'relative': 0.30; 'primary': 0.31; "can't": 0.32; 'language.': 0.32; 'run': 0.33; 'problem': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'running': 0.34; 'trouble': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'mode': 0.36; 'modules': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'whom': 0.37; 'difference': 0.38; 'url:en': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'more': 0.63; '_o__)': 0.84; 'god,': 0.84; 'handicap': 0.84; 'received:125': 0.84; 'url:latest': 0.91; 'angry': 0.93
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
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 <85iny2agyr.fsf@benfinney.id.au>
X-Mailman-Original-References <CAPxRSnb=nEEjR63iNRJyepXo+aG6d+U7H2k5E_FbedC35tcsZQ@mail.gmail.com>
Xref csiph.com comp.lang.python:109100

Show key headers only | View raw


Gerald Britton <gerald.britton@gmail.com> writes:

> On Wed, 25 May 2016 10:00 am, Steven D'Aprano wrote:
> >The problem is that you are running the Python script from *inside*
> >the package. That means, as far as the script can see, there is no
> >longer a package visible -- it cannot see its own outside from the
> >inside.
>
> Thanks for the explanation, Steven. I'm just having trouble
> reconciling this with the docs which seems to imply that an intra
> package import should work from inside the package.

The relative import will work fine inside a package.

The key difference is in *whether* your module is inside that package at
run time.

You have hit upon one of my primary complaints about Python. It makes
this normal mode of running a script::

    python3 ./foo.py

not work properly, because Python in that case has no idea in which
package the module belongs. So it can't import other modules relative to
the current directory.

What the Python import system expects you to do is::

    cd ../
    python3 -m fnord.foo

To me, that makes Python at a *severe* handicap as a simple-to-use
scripting language.

But apparently “address a module by filesystem path” is severely
deprecated in Python. From that follows a lot of problems, such as this
one.


For more on traps with Python's import system, see
<URL:http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html>

-- 
 \       “I cannot be angry at God, in whom I do not believe.” —Simone |
  `\                                                       De Beauvoir |
_o__)                                                                  |
Ben Finney

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


Thread

Re: Question about imports and packages Ben Finney <ben+python@benfinney.id.au> - 2016-05-25 14:39 +1000
  Re: Question about imports and packages Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-05-25 18:27 +1000
    Re: Question about imports and packages Chris Angelico <rosuav@gmail.com> - 2016-05-25 18:34 +1000

csiph-web