[Python-Dev] New relative import issue
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.
/foo/bar/imp1.py <-- has a "from . import imp2"
if I now put a test-case (or any other module I'd like as the main module) at:
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:
with the main.py doing a "from . import string", which is what I was
trying to accomplish...
More information about the Python-Dev