【问题标题】:RefactoringTool: ParseError: bad input: type=22, value='='RefactoringTool: ParseError: bad input: type=22, value='='
【发布时间】:2019-08-13 10:16:35
【问题描述】:

我正在重构一些 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 python-2to3


【解决方案1】:

问题在于您尝试转换的代码不是有效的 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。

【讨论】:

  • 哦!谢谢!与此同时,我发现了这个:bugs.python.org/issue35260,但仍然无法理解问题所在。
  • @cmd,该错误报告的第一个响应实际上是这样说的:“2to3 需要一个有效的 Python 2 文件作为输入,而文件 a.py 不是一个有效的 Python 2 文件:在没有from __future__ import print_function 的情况下,print 行是一个 SyntaxError。所以是的,这个 被视为 Python 2 代码,但这就是 2to3 的设计目的。”
【解决方案2】:

我发现绝对导入寻址为我排序。语法一切正常,但以下的相对导入给出了错误。

失败:

from . import classes.utility as util

作品:

from classes import utility as util

这可能只是我对 Python3 中的导入缺乏了解。

【讨论】:

    【解决方案3】:

    如果您已经将print 语句转换为函数(如您所做的那样),则可以在调用2to3 时使用-p 参数

    -p, --print-function 修改语法,使得 print() 是一个 功能

    例如

    2to3 -p yourfile.py
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 2022-01-13
      • 2014-01-09
      • 2020-04-26
      • 2023-03-19
      • 1970-01-01
      • 2013-06-28
      相关资源
      最近更新 更多