[Python-Dev] New relative import issue

Fabio Zadrozny fabiofz at gmail.com
Sun Sep 17 20:38:42 CEST 2006


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...

When you do a from . import xxx, it will always fail if you're in a
top-level module, and when executing any module, the directory of the
module will automatically go into the pythonpath, thus making all the
relative imports in that structure fail.

E.g.:

/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.

The solutions I see would be:
- only use the pythonpath actually defined by the user (and don't put
the current directory in the pythonpath)
- make relative imports work even if they reach some directory in the
pythonpath (making it work as an absolute import that would only
search the current directory structure)

Or is this actually a bug? (I'm with python 2.5 rc2)

I took another look at http://docs.python.org/dev/whatsnew/pep-328.html
and the example shows:

pkg/
pkg/__init__.py
pkg/main.py
pkg/string.py

with the main.py doing a "from . import string", which is what I was
trying to accomplish...

Cheers,

Fabio


More information about the Python-Dev mailing list