[issue6129] 2to3 does not convert imports of the form 'import sub.mod' to relative import
New submission from Mitchell Model m...@acm.org: 2to3 changes import m to from . import m but it doesn't change import sub.s. The directory q.py is in has a file m.py and a subdirectory sub. The subdirectory sub has a file s.py. File q.py contains two lines: import m import sub.s % 2to3 -f q.py --- q.py (original) +++ q.py (refactored) @@ -1,2 +1,2 @@ -import m +from . import m import sub.s RefactoringTool: Files that need to be modified: RefactoringTool: q.py -- messages: 88444 nosy: MLModel severity: normal status: open title: 2to3 does not convert imports of the form 'import sub.mod' to relative import ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6129 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6129] 2to3 does not convert imports of the form 'import sub.mod' to relative import
Georg Brandl ge...@python.org added the comment: Is there also a sub/__init__.py? -- nosy: +georg.brandl ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6129 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6129] 2to3 does not convert imports of the form 'import sub.mod' to relative import
Mitchell Model m...@acm.org added the comment: Yes, I meant to mention that: the directory sub does contain an __init__.py with __all__ = ['s']. I'm attaching a zip of the test package pkg that contains the sub-package sub. It wasn't clear to me what import sub.s should be converted to. By analogy with the conversion of import m to from . import m, I had expected import sub.s to be converted from . import sub.s. However, that is a syntax error (which I don't understand, actually). What works is from sub import s but that could import from a sub module found anywhere; it isn't changed to a relative import. I don't see how to write a relative import of a subpackage's module. I don't doubt that I am confused about something here, but I've experimented with many variations and can't get it straightened out. -- Added file: http://bugs.python.org/file14095/pkg.zip ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6129 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6129] 2to3 does not convert imports of the form 'import sub.mod' to relative import
Benjamin Peterson benja...@python.org added the comment: This is a case that 2to3 cannot handle. There is no way to do a relative import and get the module package.module behavior of absolute import. 2to3 cannot guess how to fix this. -- nosy: +benjamin.peterson resolution: - wont fix status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6129 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6129] 2to3 does not convert imports of the form 'import sub.mod' to relative import
Mitchell Model m...@acm.org added the comment: Thanks. I sure couldn't find a way! I encountered this trying to convert someone else's code. There is a main directory and a subdirectory, both with __init__.py files. The files in the main directory import each other just by import othermodule. 2to3 changed those to from . import othermodule, which works. There is a module in the sub-package, say 'm', which files in the main package want to import, which they were doing by import subdirectory.m. I'm glad 2to3 can't figure out what to do and, as per your explanation, I guess there isn't anyway to make this work. So I have to ask, though perhaps this isn't the place, how would a module in a package import a module from a sub-package of that package (where the sub-package includes the module in its all list). I've tried a lot of variations and am not doing any better than 2to3 on this. Suggestions? -- status: closed - open ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6129 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue6129] 2to3 does not convert imports of the form 'import sub.mod' to relative import
Benjamin Peterson benja...@python.org added the comment: The bug tracker is not really the correct forum for this. [See the python-porting list.] You will either have to change the code to use absolute from imports and change the usage of the module in the code base (I would suggest this), or hack it like this: import fullpackage.subpack.mod subpack = fullpackage.subpack -- status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue6129 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com