[Python-Dev] New relative import issue

Nick Coghlan ncoghlan at gmail.com
Mon Sep 18 13:25:03 CEST 2006


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

Correct. Relative imports are based on __name__ and don't work properly if 
__name__ does not properly reflect the module's position in the package 
hierarchy (usually because the module is the main module, so name is set to 
'__main__').

This is noted briefly in PEP 328 [1], with the current workarounds explained 
in more detail in PEP 338 [2].

Cheers,
Nick.

[1]
http://www.python.org/dev/peps/pep-0328/#relative-imports-and-name

[2]
http://www.python.org/dev/peps/pep-0338/#import-statements-and-the-main-module

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list