[New-bugs-announce] [issue8358] absolute_import future directive ignored by 2to3

Jason R. Coombs report at bugs.python.org
Fri Apr 9 19:09:50 CEST 2010

New submission from Jason R. Coombs <jaraco at jaraco.com>:

I'm using Python 3.1.2 64-bit on Windows.

I've found that even if "absolute_import" is imported from __future__, 2to3 will convert imports to be treated as relative.

To demonstrate this behavior, I created a small package "abs_imp_test" (attached).

abs_imp_test.__init__ is 0 bytes.
abs_imp_test.string is a one-line file.
  foo = 'bar'
abs_imp_test.main contains 4 lines:
  from __future__ import absolute_import
  import string
  assert not hasattr(string, 'foo'), 'fail'

Put abs_imp_test package somewhere in the python path (just having it relative to current directory works).

Note that the code is designed to be future-proof (using the future directive), so will run under Python 2.6 and Python 3.1 without errors.

> python26\python -c "from abs_imp_test import main"
> python31\python -c "from abs_imp_test import main"

However, if I run 2to3 on main, it converts "import string" to "from . import string" which changes the fundamental meaning of the import and breaks the test.

> 2to3 abs_import_test
RefactoringTool: Files that were modified:
RefactoringTool: abs_imp_test\main.py

> python -c "from abs_imp_test import main"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "abs_imp_test\main.py", line 4, in <module>
    assert not hasattr(string, 'foo'), "fail"
AssertionError: fail

Is it possible that if the absolute_import future directive is present that the imports not be modified for relativity?

components: 2to3 (2.x to 3.0 conversion tool)
files: abs_imp_test.zip
messages: 102733
nosy: jaraco
severity: normal
status: open
title: absolute_import future directive ignored by 2to3
versions: Python 3.1
Added file: http://bugs.python.org/file16846/abs_imp_test.zip

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list