我正在重构一些python2代码并使用2to3模块将其更改为python3。我收到以下解析错误:
RefactoringTool: There was 1 error:
RefactoringTool: Can't parse ./helpers/repo.py: ParseError: bad input: type=22, value='=', context=(' ', (45, 25))
下面是产生错误的代码:
except ImportError as error_msg: # pragma: no cover
print(' ', file = sys.stderr) # this is a line that yields error
print("Could not locate modifyrepo.py", file=sys.stderr)
print("That is odd... should be with createrepo", file=sys.stderr)
raise ImportError(error_msg)
我不知道可能出了什么问题。你能帮忙吗?
问题是您尝试转换的代码不是有效的 Python 2 代码。
使用 Python 2 运行代码时,会出现以下错误:
File "repo.py", line 5
print(' ', file = sys.stderr) # this is a line that yields error
^
SyntaxError: invalid syntax
似乎这段代码已经是 Python 3 代码了。使用 Python 3,您的代码不会产生 SyntaxError。
如果您已经将print
语句转换为函数(就像您所做的那样(,则可以在调用2to3
时使用-p
参数
-p, --print-function 修改语法,使 print(( 是一个 功能
例如
2to3 -p yourfile.py
我发现绝对导入寻址为我解决了这个问题。 语法都很好,但以下内容的相对导入给出了错误。
失败:
from . import classes.utility as util
工程:
from classes import utility as util
这可能只是我对 Python3 中的导入缺乏了解。
我遇到了类似的问题。 我的打印语句已经转换为函数。
问题是打印功能的导入是作为
from __future__ import (
unicode_literals,
print_function,
)
为了修复它,我必须将导入放在单独的专用行上
from __future__ import print_function
希望对你有帮助