[Python-Dev] Another relative imports question

Georg Brandl g.brandl at gmx.net
Fri Oct 8 12:31:49 CEST 2010


Am 08.10.2010 10:50, schrieb Chris Withers:
> Hi All,
> 
> The new explicit relative import syntax is great.
> I wanted to relatively import a module.
> 
> import .mymoduleinmypackage
> 
> ....and got a SyntaxError in Python 2.6.
> 
> I guess I need to do:
> 
> from . import mymoduleinmypackage
> 
> ....but it does feel weirdly asymetric that:
> 
> from .mymoduleinmypackage import something
> 
> ....while:
> 
> import .mymoduleinmypackage
> mymoduleinmypackage.something
> 
> ....does not.

The explanation is that everything that comes after "import" is thereafter
usable as an identifier (or expression, in the case of dotted names) in
code.  ".mymodule" is not a valid expression, so the question would be how
to refer to it.

(This argument is weakened in the present of an "as" renaming, but I doubt
you'd like to write

import .mymodule as mymodule

.)

Georg

-- 
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.



More information about the Python-Dev mailing list