[Python-Dev] Package ambiguities

Gordon McMillan gmcm@hypernet.com
Wed, 7 Jun 2000 17:59:20 -0400


Ka-Ping wrote:

> On Wed, 7 Jun 2000, Andy Robinson wrote:
> > We hit some very weird behaviour recently while setting up a
> > package hierarchy.  Robin Becker managed to distil this into a
> > simple example. Can anyone shed any light on what is happening
> > below?  Is Python behaving as it should?

[snip entirely correct analysis]

> The solution is to avoid directly running scripts that are
> inside packages; the confusion is produced by the fact that
> you're running "test.py" from within the A/ directory.

I disagree with the moral.

If test.py uses "import A.parent" and changes references 
accordingly, it works.

So my version of the moral is that import * is EVIL. Not only 
that, but relative imports are EVIL.

This is what I meant when I said (in reply to Greg Wilson's 
inquiry about package structures) that there are some very 
bad precedents getting set.

Doing this junk (usually in __init__.py) not only makes your 
package fragile, it also makes the package structure 
mysterious to the user (who's a developer - making things 
mysterious to him/her is just making both your lives harder!). If 
you want to make it friendly to lazy coders, then something 
like:

import A.parent
Parent = A.parent.Parent

also works, (leaving references as Parent). I'm not terribly fond 
of this, either, but at least the coder has a reasonable hope of 
tracking down what "Parent" actually is.

I'm not necessarily yelling at you, Andy. It seems that most 
package developers are going down this slope. It stinks.


- Gordon