Hi! 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. 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__ ... An alternative syntax would be "import .systemmodule", but that seems obscure. Ciao, Jürgen
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
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.
You did the right thing, for a good reason.
So my question is: is there an easy way to force a top-level import (adress the top-level namespace), that I overlooked?
No.
If not, what do you think of a
from __top__ import systemmodule
similar to from __future__ ...
An alternative syntax would be "import .systemmodule", but that seems obscure.
I'm not keen on this. I'd rather deprecate relative imports -- they cause more trouble than they're worth. --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (3)
-
Brian Quinlan
-
Guido van Rossum
-
j.her@t-online.de