[Python-Dev] New relative import issue

Armin Rigo arigo at tunes.org
Mon Sep 18 11:13:14 CEST 2006


Hi Fabio,

On Sun, Sep 17, 2006 at 03:38:42PM -0300, Fabio Zadrozny wrote:
> I've been playing with the new features and there's one thing about
> the new relative import that I find a little strange and I'm not sure
> this was intended...

My (limited) understanding of the motivation for relative imports is
that they are only here as a transitional feature.  Fully-absolute
imports are the official future.

Neither relative nor fully-absolute imports address the fact that in any
multi-package project I've been involved with, there is some kind of
sys.path hackery required (or even custom import hooks).  Indeed, there
is no clean way from a test module 'foo.bar.test.test_hello' to import
'foo.bar.hello': the top-level directory must first be inserted into
sys.path magically.

> /foo/bar/imp1.py <-- has a "from . import imp2"
> /foo/bar/imp2.py
> 
> if I now put a test-case (or any other module I'd like as the main module) at:
> /foo/bar/mytest.py
> 
> if it imports imp1, it will always fail.

Indeed: foo/bar/mytest.py must do 'import foo.bar.imp1' or 'from foo.bar
import imp1', and then it works (if sys.path was properly hacked first,
of course).  (I'm not sure, but I think that this not so much a language
design decision as a consequence of the complexities of import.c, which
is the largest C source file of CPython and steadily growing.)


A bientot,

Armin


More information about the Python-Dev mailing list