views:

32

answers:

1

I have a project like that :

foo/
| main.py
| bar/
| | module1.py
| | module2.py
| | __init__.py

with main.py doing import bar.module1 and module1.py doing import module2.

This works using python 2.6 but not with python 3.1 (ImportError: No module named module2)

Why did the behaviour change ? How to restore it ?

+1  A: 

In module1.py, do a: from . import module2

main.py

import bar.module1
print(bar.module1.module2.thing)

bar/init.py

#

bar/module1.py

#import module2 # fails in python31
from . import module2 # intrapackage reference, works in python26 and python31

bar/module2.py

thing = "blah"

As for why/how, that's above my paygrade. The documentation doesn't seem to elucidate it. Maybe in Python 3 they decided to enforce submodules in packages being explicitly imported with the intrapackage style?

Nick T
Thanks a lot for the pointers !
Scharron
Also referenced in [What's new in Python 3.0](http://docs.python.org/release/3.0.1/whatsnew/3.0.html): "The only acceptable syntax for relative imports is `from .[module] import name`. All import forms not starting with `.` are interpreted as absolute imports."
ire_and_curses