SyntaxError:带有变量注释的语法无效

Jas*_*Jas 8 python python-3.x visual-studio-code

我已经安装了visual studio代码和code runner扩展.然后我有这段代码:

text: str = "slkdfjsd"
Run Code Online (Sandbox Code Playgroud)

我点击了CTRL-ALT-N,我得到:

    text: str = "slkdfjsd"
        ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我喜欢使用类型,这个程序工作,看起来它抱怨类型我怎么能理解类型是好的?

更多细节:

$ /usr/bin/env python3 --version
Python 3.6.6 :: Anaconda, Inc.
Run Code Online (Sandbox Code Playgroud)

当它运行时:

[Running] /usr/bin/env python3 "/home/myuser/dev/projects/python-snippets/text-summarization"
  File "/home/myuser/dev/projects/python-snippets/text-summarization", line 44
    text: str = "slkdfjsd"
        ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

Code runner插件文档说:

$pythonPath:Python解释器的路径(设置Python: Select Interpreter command)

但是当我按照评论建议运行打印路径时,我得到了一个不同的版本:

sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
Run Code Online (Sandbox Code Playgroud)

这是不同的,你可以从>python: select interpreter我选择的上面看到.

还要注意的是,当我在终端,而不是与运行运行Visual Studio代码的代码CTRL-ALT-N,然后选择Python版本是3.6,它运行没有任何语法错误精细,所以我觉得这件事情的代码亚军没有看到相同的Python版本我在选择时看到的>python: select interpreter

更新:我看到的确是码流道采用如上所述错误的Python解释器,所以我打开了我的用户设置,并试图更新python为指向正确的解释不过它并没有改变它仍然使用相同的错误解释在这里一切皆有我试过的:

{
    "git.autofetch": true,
    "terminal.integrated.rendererType": "dom",
    "code-runner.executorMap": {
        "javascript": "node",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "php": "php",
        "python": "/home/user/home/user/dev/anaconda3/envs/pymachine/bin/python",
        "perl": "perl",
        "perl6": "perl6",
        "ruby": "ruby",
        "go": "go run",
        "lua": "lua",
        "groovy": "groovy",
        "powershell": "powershell -ExecutionPolicy ByPass -File",
        "bat": "cmd /c",
        "shellscript": "bash",
        "fsharp": "fsi",
        "csharp": "scriptcs",
        "vbscript": "cscript //Nologo",
        "typescript": "ts-node",
        "coffeescript": "coffee",
        "scala": "scala",
        "swift": "swift",
        "julia": "julia",
        "crystal": "crystal",
        "ocaml": "ocaml",
        "r": "Rscript",
        "applescript": "osascript",
        "clojure": "lein exec",
        "haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
        "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
        "racket": "racket",
        "ahk": "autohotkey",
        "autoit": "autoit3",
        "dart": "dart",
        "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
        "haskell": "runhaskell",
        "nim": "nim compile --verbosity:0 --hints:off --run"
      }
}
Run Code Online (Sandbox Code Playgroud)

然而,在改变之后(也许我有点错误,我是vscode新手)我仍然看到代码运行器正在运行它:

[Running] /usr/bin/env python3 "/home/myuser/dev/projects/python-snippets/text-summarization"
Run Code Online (Sandbox Code Playgroud)

abc*_*ccd 5

注意:以下答案假定您使用的是正确的版本 ( 3.6+ ),否则:简单地说,您当前版本的 Python 不支持变量注释。


问题似乎是类型注释导致了SyntaxError,但另一种非常合理的可能性是在前面的行中有一个未封闭的括号或未封闭的东西。因为在docs,它说:

解析器重复违规行并显示一个小“箭头”,指向检测到错误的行中最早的点。错误是由(或至少检测到)箭头前面的标记引起的

(强调我的)

当给出在该上下文中无效的标记时,解析器只能检测未闭合的括号。由于方括号和圆括号可以包含多行(这意味着不会引发 EOL),并且text是有效的变量标识符,因此只剩下方括号或圆括号中不允许使用冒号(当它用作参数时除外,即也接受类型注释)。

这是托管在 tio.run(SE code-golf 编译器)上的代码的可重现示例:

https://tio.run/##K6gsycjPM/7/X4OrJLWixEqhuKRIwVZBXf3/fwA

(
text: str = ''
Run Code Online (Sandbox Code Playgroud)

:是在这方面令牌第一无效。

  File ".code.tio", line 2
    text: str = ''
        ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

如果您有一个未关闭的 dict,其中允许使用冒号,则箭头将指向其他地方,因为text: str所有有效标记都是由开头处理的{。指针将指向等号,因为这是第一个无效标记。

{
text: str = ''
Run Code Online (Sandbox Code Playgroud)

和例外:

  File ".code.tio", line 2
    text: str = ''
              ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

  • 由于可能有多种可能的答案,我将保留我的答案。尽管它可能对您没有帮助,但它可能会对遇到我所描述的问题的人有所帮助。太好了,你自己找到了原因。 (2认同)