confusion about package/module imports

Jugdish danebarney at gmail.com
Tue Jan 1 21:26:38 CET 2008


Why doesn't the following work?

>>> ls $HOME
$HOME/pkg/__init__.py
$HOME/pkg/subpkg/__init__.py
$HOME/pkg/subpkg/a.py
$HOME/pkg/subpkg/b.py

>>> cat $HOME/pkg/__init__.py
# empty

>>> cat $HOME/pkg/subpkg/__init__.py
import a
import b

>>> cat $HOME/pkg/subpkg/a.py
class A:
    pass

>>> cat $HOME/pkg/subpkg/b.py
import pkg.subpkg.a
class B(pkg.subpkg.a.A):
    pass

>>> setenv PYTHONPATH $HOME:$PYTHONPATH
>>> python $HOME/pkg/subpkg/b.py
Traceback (most recent call last):
  File "pkg/subpkg/b.py", line 1, in ?
    import pkg.subpkg.a
  File "$HOME/pkg/subpkg/__init__.py", line 2, in ?
    import b
  File "$HOME/pkg/subpkg/b.py", line 2, in ?
    class B(pkg.subpkg.a.A):
AttributeError: 'module' object has no attribute 'subpkg'



More information about the Python-list mailing list