0

我的代码在 Python 3.8 中完美运行,但是当我在相同的操作系统中切换到 Python 3.5,使用相同的代码和其他所有内容时,它开始抛出“SyntaxError:无效语法”。

这是错误,以及我认为与错误相关的代码部分:

Traceback (most recent call last):
  File "pwb.py", line 390, in <module>
    if not main():
  File "pwb.py", line 385, in main
    file_package)
  File "pwb.py", line 100, in run_python_file
    exec(compile(source, filename, 'exec', dont_inherit=True),
  File ".\scripts\signbot.py", line 83
    namespace: int
             ^
SyntaxError: invalid syntax
CRITICAL: Exiting due to uncaught exception <class 'SyntaxError'>

这是代码的一部分:

@dataclass
class RevisionInfo:
    namespace: int
    title: str
    type: str
    bot: bool
    comment: str
    user: str
    oldRevision: Optional[int]
    newRevision: int
    timestamp: int

抱歉,如果问题标题不具体,但我无法让此代码在 Python 3.5 中运行。我将在其中运行此代码的服务器仅支持 Python 3.5,因此我需要使其与 3.5 一起使用。谢谢。

4

2 回答 2

1

这里至少有两个问题:

  1. 变量注释是 Python 3.6 中的新功能。

  2. dataclasses模块是 Python 3.7 中的新模块。

要么使用 Python 3.7 或更高版本,要么重写代码,使其不依赖于数据类和变量注释。

这是在开发中使用与您打算在生产中使用的相同版本的 Python 是个好主意的众多原因之一。您可以避免编写无法在您的服务器上运行的代码。

于 2020-08-07T12:26:31.963 回答
-1

Python 3.7 中一个令人兴奋的新特性是数据类。你不能在 python 3.5 中使用它。您应该使用传统方式并使用构造函数:

class Mapping:
def __init__(self, iterable):
    self.items_list = []
    self.__update(iterable)

def update(self, iterable):
    for item in iterable:
        self.items_list.append(item)
于 2020-08-07T12:27:10.637 回答