[Python-Dev] relative import circular problem

Kristján Valur Jónsson kristjan at ccpgames.com
Tue Apr 2 11:24:37 CEST 2013


> -----Original Message-----
> From: Python-Dev [mailto:python-dev-
> bounces+kristjan=ccpgames.com at python.org] On Behalf Of Barry Warsaw
> Sent: 1. apríl 2013 22:16
> To: python-dev at python.org
> Subject: Re: [Python-Dev] relative import circular problem
> 
> On Apr 01, 2013, at 08:20 PM, Kristján Valur Jónsson wrote:
> 
> >The relative import syntax
> >
> > (from foo import bar) is a getattr type of lookup (i.e. import foo,
> > then get attr from it).
> >
> >This is in contrast with absolute import
> >
> >  import foo.bar  (get the module foo.bar from sys.modules or import
> > it)
> >
> >  bar = foo.bar
> 
> I always thought of both syntaxes as absolute imports because they both
> name the full dotted path to the module you want.  This contrasts with true
> PEP
> 328 relative imports such as:
> 
>     from ..foo import bar
> 
Right, the example I gave was not a relative import but an absolute one, relative imports always
starting with a dot.  I should have been more clear.
However, relative imports can _only_ be performed using the "from X import Y syntax" (http://www.python.org/dev/peps/pep-0328/#id3)
and so, when one wishes to use relative imports, the parent module must be fully initialized with the child module as an attribute.
importing with the "import X.Y" does not appear to have this restriction.

> I personally dislike PEP 328 relative imports, since they seem fragile, but that's a different discussion.
Yes.  I find them very useful.  In our environment, we tend to write packages that then go into
different places.  Using relative imports allows us the freedom to move packages around and rename
them, for example, make a package a sub-package of another, and do this differently for different products, while still sharing code.
A package can thus refer to internals of itself, without knowing its own name, or absolute path.  This is really useful.

Consider this a property of Python-based "products", with a custom assembly of source code, as opposed to publicly
available packages that always install into the sys.path

Cheers,
K


More information about the Python-Dev mailing list