469,903 Members | 1,463 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Relative import bug or not?

After reading PEP-0328 I wanted to give relative imports a try:

# somepkg/__init__.py
<empty>

# somepkg/test1.py
from __future__ import absolute_import
from . import test2

if __name__ == "__main__":
print "Test"

# somepkg/test2.py
<empty>

But it complaints:
C:\1\somepkg>test1.py
Traceback (most recent call last):
File "C:\1\somepkg\test1.py", line 1, in <module>
from . import test2
ValueError: Attempted relative import in non-package

Does this mean that packages that implement self tests are not allowed
to use relative import? Or is it just a bug? I can understand that I
can use "import test2" when it's __main__, but when it's not now it
complains about no module test2 with absolute_import on.

PEP-0328 also has this phrase: "Relative imports use a module's
__name__ attribute to determine that module's position in the package
hierarchy. If the module's name does not contain any package
information (e.g. it is set to '__main__') then relative imports are
resolved as if the module were a top level module, regardless of where
the module is actually located on the file system.", but maybe my
english knowledge is not really good, because I can't understand what
should actually happen here ("relative imports are resolved as if the
module were a top level module")... :-/

So is it a bug, or am I doing something wrong?

Oct 14 '06 #1
1 5516

Alexey Borzenkov wrote:
After reading PEP-0328 I wanted to give relative imports a try:

# somepkg/__init__.py
<empty>

# somepkg/test1.py
from __future__ import absolute_import
from . import test2

if __name__ == "__main__":
print "Test"

# somepkg/test2.py
<empty>

But it complaints:
C:\1\somepkg>test1.py
Traceback (most recent call last):
File "C:\1\somepkg\test1.py", line 1, in <module>
from . import test2
ValueError: Attempted relative import in non-package

Does this mean that packages that implement self tests are not allowed
to use relative import? Or is it just a bug? I can understand that I
can use "import test2" when it's __main__, but when it's not now it
complains about no module test2 with absolute_import on.

PEP-0328 also has this phrase: "Relative imports use a module's
__name__ attribute to determine that module's position in the package
hierarchy. If the module's name does not contain any package
information (e.g. it is set to '__main__') then relative imports are
resolved as if the module were a top level module, regardless of where
the module is actually located on the file system.", but maybe my
english knowledge is not really good, because I can't understand what
should actually happen here ("relative imports are resolved as if the
module were a top level module")... :-/

So is it a bug, or am I doing something wrong?

Short version is: relative imports do not work in 2.5 when a script is
run as "__main__"

Jürgen

Oct 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Rutger Claes | last post: by
reply views Thread by Anders J. Munch | last post: by
12 posts views Thread by Alan Isaac | last post: by
reply views Thread by Alan Isaac | last post: by
9 posts views Thread by rbygscrsepda | last post: by
reply views Thread by Kay Schluehr | last post: by
7 posts views Thread by Robert Hancock | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.