intra-package mutual imports fail: "from <pkg> import <mod>"
David Goodger
goodger at users.sourceforge.net
Tue May 28 21:37:27 EDT 2002
I ran across this wrinkle and hope that someone can shed some light.
I have a package on sys.path containing pairs of modules, each
importing the other::
package/
__init__.py:
# empty
module1.py:
import module2 # relative import
module2.py:
import module1
Executing "from package import module1" works fine. Changing the
import statements to absolute dotted forms also works for "from
package import module3"::
module3.py:
import package.module4 # absolute import
module4.py:
import package.module3
However, if I change both imports to be absolute using the
"from/import" form, it doesn't work::
module5.py:
from package import module6 # absolute import
module6.py:
from package import module5
Now I get an exception::
>>> from package import module5
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "package/module5.py", line 1, in ?
from package import module6
File "package/module6.py", line 1, in ?
from package import module5
ImportError: cannot import name module5
Is this behavior expected? Or is it a bug? I note that FAQ entry
4.37 [*]_ says we shouldn't do "from <module> import *"; I'm not. Are
all "from import" statements forbidden in this context? Why? (It
seems to me that "import package.module" and "from package import
module" are equivalent imports, except for their effect on the local
namespace.) Is there an authoritative reference (docs, past c.l.p
post, bug report, etc.)?
.. [*] http://www.python.org/cgi-bin/faqw.py?req=show&file=faq04.037.htp
--
David Goodger <goodger at users.sourceforge.net> Open-source projects:
- Python Docutils: http://docutils.sourceforge.net/
(includes reStructuredText: http://docutils.sf.net/rst.html)
- The Go Tools Project: http://gotools.sourceforge.net/
More information about the Python-list
mailing list