[Python-Dev] Importing top-level modules

Brian Quinlan brian@sweetapp.com
Tue, 16 Apr 2002 15:00:41 -0700


Juergen wrote:
> I had a problem yesterday where I wanted to extend a top-level module
> by a module in a subpackage. I naively named my module the same as the
> toplevel module, and peer import bit me. I finally had to rename my
> module for no good reason to get things working.

What you are trying to do seems likely to cause confusion. But if you
really need this, couldn't you just mangle sys.path for a single import
and then restore it? It's kind of ugly but it would work.

> So my question is: is there an easy way to force a top-level import
> (adress the top-level namespace), that I overlooked?
> 
> If not, what do you think of a
> 
> 	from __top__ import systemmodule
> 
> similar to from __future__ ...

I don't like this idea. I'd hate to encourage people to give their
subpackages the same name as system modules:

from package import string
# many lines of code later
string.do_something_wierd() # what?!?

> An alternative syntax would be "import .systemmodule", but that seems
> obscure.

That's grotesque. Especially since I think of a leading period as
meaning current directory :-)

Cheers,
Brian