[New-bugs-announce] [issue22425] 2to3 import fixer writes dotted_as_names into import_as_names

Simon Weber report at bugs.python.org
Tue Sep 16 23:00:27 CEST 2014


New submission from Simon Weber:

When dealing with implicit relative imports of the form "import <dotted_as_names> as...", the 2to3 import fixer will rewrite them as "from . import <dotted_as_names> as...".

This is invalid syntax. Here's an example:

$ tree package/
package/
├── __init__.py
├── rootmod.py
└── subpackage
    ├── __init__.py
    └── mod.py

1 directory, 4 files

$ cat package/rootmod.py  # this is the only nonempty file
import subpackage.mod as my_name

$ python package/rootmod.py

$ 2to3 -w -f import package/
RefactoringTool: Refactored package/rootmod.py
--- package/rootmod.py	(original)
+++ package/rootmod.py	(refactored)
@@ -1 +1 @@
-import subpackage.mod as my_name
+from . import subpackage.mod as my_name
RefactoringTool: Files that were modified:
RefactoringTool: package/rootmod.py

$ python package/rootmod.py
  File "package/rootmod.py", line 1
    from . import subpackage.mod as my_name
                            ^
SyntaxError: invalid syntax

Probably the easiest way to rewrite this is "from .subpackage import mod as my_name".

----------
components: 2to3 (2.x to 3.x conversion tool)
messages: 226965
nosy: simonmweber
priority: normal
severity: normal
status: open
title: 2to3 import fixer writes dotted_as_names into import_as_names
type: behavior
versions: Python 2.7, Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22425>
_______________________________________


More information about the New-bugs-announce mailing list