Re: [Python-Dev] intra-package mutual imports fail: "from <pkg> import <mod>"
module5.py: from package import module6 # absolute import
module6.py: from package import module5 [...] ImportError: cannot import name module5
Is this behavior expected? Or is it a bug?
The problem is that importing with from consists of two steps: - load the module - add the imported names to the local namespace Since this addition is by reference to the actual object and not to the symbol's name in the other module, a concept which Python doesn't have (use Perl if you want this...), your recursive import doesn't work. The solution would be: import package.module6 as module6 which should have the same effect. -- Matthias Urlichs
module5.py: from package import module6 # absolute import
module6.py: from package import module5 [...] ImportError: cannot import name module5
Is this behavior expected? Or is it a bug?
The problem is that importing with from consists of two steps: - load the module - add the imported names to the local namespace
Good explanation! This means it's an unavoidable problem. Maybe you can fix the FAQ entry? --Guido van Rossum (home page: http://www.python.org/~guido/)
Hi, Guido van Rossum:
Good explanation! This means it's an unavoidable problem. Maybe you can fix the FAQ entry?
I've rewritten FAQ 4.37, though I just noticed that I mis-pasted the log entry (it's incomplete). I'll be more careful in the future. :-/ -- Matthias Urlichs | noris network AG | http://smurf.noris.de/
On 2 Jun 2002 at 9:25, Guido van Rossum wrote: [Matthias Urlichs]
The problem is that importing with from consists of two steps: - load the module - add the imported names to the local namespace
Good explanation! This means it's an unavoidable problem.
Um, different problem. What Matthias explains is unavoidable. But in David's case, the containing namespace (the package) is not empty when module2 wants module1. In fact, I believe that sys.modules['package.module1'] is there (though *it* is empty). My guess is that import is looking for module1 as an attribute of package, and that that binding hasn't taken place yet. If I use iu instead of the builtin import, it works. -- Gordon http://www.mcmillan-inc.com/
Um, different problem. What Matthias explains is unavoidable. But in David's case, the containing namespace (the package) is not empty when module2 wants module1. In fact, I believe that sys.modules['package.module1'] is there (though *it* is empty).
My guess is that import is looking for module1 as an attribute of package, and that that binding hasn't taken place yet.
Yes, that's what "from package import module" does -- it wants "module" to be an attribute of "package". This is because it doesn't really distinguish between "from package import module" and "from module import attribute". --Guido van Rossum (home page: http://www.python.org/~guido/)
I wrote:
module5.py: from package import module6 # absolute import
module6.py: from package import module5 [...] ImportError: cannot import name module5
Is this behavior expected? Or is it a bug?
Matthias Urlichs replied:
The problem is that importing with from consists of two steps: - load the module - add the imported names to the local namespace
Since this addition is by reference to the actual object and not to the symbol's name in the other module, a concept which Python doesn't have (use Perl if you want this...), your recursive import doesn't work.
The solution would be: import package.module6 as module6
which should have the same effect.
Perhaps I'm just dense, or perhaps it's because of my choice of names
in my example, but I don't understand the explanation. Could you be
more specific, perhaps with a concrete example? Despite Guido's
"Good explanation!", the above text in the FAQ entry wouldn't
eliminate my confusion. I suspect it's a good explanation for those
that already understand what's going on behind the scenes.
--
David Goodger
From: "David Goodger"
Perhaps I'm just dense, or perhaps it's because of my choice of names in my example, but I don't understand the explanation. Could you be more specific, perhaps with a concrete example? Despite Guido's "Good explanation!", the above text in the FAQ entry wouldn't eliminate my confusion.
Nor mine, FWIW. -D
Hi, David Goodger:
Perhaps I'm just dense, or perhaps it's because of my choice of names in my example, but I don't understand the explanation. Could you be more specific, perhaps with a concrete example?
foo.py: from bar import one bar.py: from foo import two main.py: import foo So what happens is, more or less: main imports foo Empty globals for foo are created foo is compiled foo loads bar Empty globals for bar are created bar is compiled bar loads foo (which is a no-op since there already is a module named foo) bar.two = foo.two ... which fails, because the compiler isn't done with foo yet and the global symbol dict for foo is still empty.
eliminate my confusion. I suspect it's a good explanation for those that already understand what's going on behind the scenes.
_If_ you can change foo.py so that it reads: two = 2 from bar import one i.e., initialize the exports first and load afterwards, the test would work. However, the following will NOT work: two = None from bar import one two = do_something(with(bar.one)) for (hopefully) obvious reasons. -- Matthias Urlichs | noris network AG | http://smurf.noris.de/
Matthias, thank you for your explanation. I was operating under the
assumption that the mechanism behind "from package import module" was
somehow different from that behind "from module import name", because there
is no name "module" inside package/__init__.py. A little experimentation
confirmed that it was a mistaken assumption. Now all is clear.
The note in section 6.12 ("The import statement") of the Language Reference,
"XXX Can't be bothered to spell this out right now", has always bothered me.
I will endeavour to flesh it out for release 2.3. Perhaps Guido's package
support essay (http://www.python.org/doc/essays/packages.html), edited,
should form a new section or appendix. Ideas anyone?
--
David Goodger
The note in section 6.12 ("The import statement") of the Language Reference, "XXX Can't be bothered to spell this out right now", has always bothered me. I will endeavour to flesh it out for release 2.3. Perhaps Guido's package support essay (http://www.python.org/doc/essays/packages.html), edited, should form a new section or appendix. Ideas anyone?
The information should be worked into the language reference IMO. I'm not sure if an appendix is appropriate or if this could be part of the documentation for the import statement. Thanks for helping out!!! --Guido van Rossum (home page: http://www.python.org/~guido/)
Matthias Urlichs
Since this addition is by reference to the actual object and not to the symbol's name in the other module, a concept which Python doesn't have (use Perl if you want this...)
Perl doesn't add references to names. It imports direct reference as well. The difference is that perl will create the named object in the exporting package when it is imported, if the exporting package's init code has not executed yet. In Perl this works because we at import time know if we are importing a variable (and what kind) or a function, and later assignments to variables or redefinitions of functions mutate the object in-place. Regards, Gisle Aas
Hi, Gisle Aas:
Perl doesn't add references to names. It imports direct reference as well.
What I meant to say was: Perl shares the actual symbol table slot when you import something; so a later reassignment to the variable in question will affect every module. Python doesn't have that additional indirection.
In Perl this works because we at import time know if we are importing a variable (and what kind) or a function, and later assignments to variables or redefinitions of functions mutate the object in-place.
... which is essentially a different way to state the same thing. ;-) -- Matthias Urlichs | noris network AG | http://smurf.noris.de/
"Matthias Urlichs"
Gisle Aas:
Perl doesn't add references to names. It imports direct reference as well.
What I meant to say was: Perl shares the actual symbol table slot when you import something;
Wrong. If you do: package Bar; use Foo qw(foo); Then you end up with \&Bar::foo and \&Foo:foo pointing to the same function object, but the symbol table slots are independent. Is is exactly the same situation you would have in Python with two namespace dicts pointing to the same function object. But you can achieve sharing by explicit import of the symbol (aka glob) using something like: use Foo qw(*foo); Regards, Gisle Aas
Hi, Gisle Aas:
What I meant to say was: Perl shares the actual symbol table slot when you import something;
Wrong. If you do:
package Bar; use Foo qw(foo);
Then you end up with \&Bar::foo and \&Foo:foo pointing to the same function object, but the symbol table slots are independent.
Right, actually; we're just miscommunicating. Let's state it differently: the situation is more like "use Foo qw($foo)" (you can't assign to &Foo::foo). After the "use", \$Bar::foo and \$Foo:foo point to the same scalar variable, thus $Foo::foo and $Bar::foo are the same variable and no longer independent (unless a different scalar or glob reference is stored in to *Foo::foo, but that's too much magic for a FAQ entry). In Python, Bar.foo gets set to the contents of Foo.foo when the import statement is processed, but the two variables are otherwise independent. -- Matthias Urlichs | noris network AG | http://smurf.noris.de/
participants (6)
-
David Abrahams
-
David Goodger
-
Gisle Aas
-
Gordon McMillan
-
Guido van Rossum
-
Matthias Urlichs