1

I have two Python packages, working and notworking. Package working has the following structure and content:

working/
    __init__.py      # from . import views
    dialogs.py       # import views
    views/
        __init__.py  # from . import main
        main.py      # from .. import dialogs

Package notworking is identical in structure and content, with one exception: it 'fixes' the non-relative import that appears in working/dialogs.py. Because we don't want to accidentally import the wrong views module, right?:

notworking/
    __init__.py      # from . import views
    dialogs.py       # from . import views  # That's better!  Right?
    views/
        __init__.py  # from . import main
        main.py      # from .. import dialogs

With this one 'improvement', module notworking no longer imports:

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import notworking
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "notworking/__init__.py", line 2, in <module>
    from . import views
  File "notworking/views/__init__.py", line 2, in <module>
    from . import main
  File "notworking/views/main.py", line 2, in <module>
    from .. import dialogs
  File "notworking/dialogs.py", line 2, in <module>
    from . import views
ImportError: cannot import name views

I would love for someone knowledgeable to explain why the absolute import works and the relative import does not work.

(Secondarily, I am open to suggestions on how to solve the mutual dependencies between dialogs and views that got me into this sticky wicket in the first place.)

davidrmcharles
  • 1,923
  • 2
  • 20
  • 33

0 Answers0