【问题标题】:SyntaxError: invalid syntax with variable annotationSyntaxError:带有变量注释的无效语法
【发布时间】:2018-12-14 15:58:45
【问题描述】:

我已经安装了 Visual Studio 代码和 code runner 扩展。然后我有这段代码:

text: str = "slkdfjsd"

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

    text: str = "slkdfjsd"
        ^
SyntaxError: invalid syntax

我喜欢使用类型,而且这个程序有效,看起来它在抱怨类型我怎么能让它明白类型是好的?

更多细节:

$ /usr/bin/env python3 --version
Python 3.6.6 :: Anaconda, Inc.

当它运行时:

[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

代码运行器插件文档说:

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

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

sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)

正如您在上面看到的,与我选择的 >python: select interpreter 不同。

还请注意,当我在 Visual Studio 代码中运行此代码时,在终端中运行而不是 CTRL-ALT-N,然后选择的 python 版本是 3.6,它运行良好,没有任何语法错误,所以我认为这是代码运行器的问题没有看到我在选择 >python: select interpreter 时看到的相同 python 版本

更新:我确实看到 code-runner 使用了错误的 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"
      }
}

但是在更改之后(也许我做错了什么,我是 vscode 新手)我仍然看到代码运行器正在运行它:

[Running] /usr/bin/env python3 "/home/myuser/dev/projects/python-snippets/text-summarization"

【问题讨论】:

  • 看起来您是在较旧的 Python 版本上运行代码。
  • 听起来像,但我不明白为什么 - 它说的是确切的 python 它运行它的/usr/bin/env python3 和它的版本是: `3.6.6` 如问题中所述。
  • 你能从sys.version_info确认吗?
  • 嗯,我得到了:[正在运行] /usr/bin/env python3 "/home/user/dev/projects/python-sn-ps/text-summarization.py" sys.version_info: sys. version_info(major=3,minor=5,micro=2,releaselevel='final',serial=0),我选择了带有ctrl-shift-p的python解释器,然后>python: select interperter看起来像code runner插件从不同的地方:(
  • 因此,您需要注意的事项很少。您在其中启动的终端不知道所选的解释器,因此它将使用默认的 python。我还记得看到另一个问题,其中提到扩展当前无法获取选定的解释器信息,这意味着您的扩展可能有问题。最好的办法是使用您希望运行的 python 的绝对路径。另外,如果你有虚拟环境,那么终端python默认不能使用环境python,你需要手动source它

标签: python python-3.x visual-studio-code


【解决方案1】:

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


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

解析器重复违规行并显示一个小“箭头” 指向错误所在行的最早点 检测到。错误是由令牌引起的(或至少在令牌处检测到) 在箭头之前

(强调我的)

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

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

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

(
text: str = ''

: 是该上下文中的第一个无效标记。

  File ".code.tio", line 2
    text: str = ''
        ^
SyntaxError: invalid syntax

如果您有一个未封闭的字典,但允许使用冒号,则箭头将指向其他位置,因为text: str 都是以开头{ 开头的所有有效标记。指针将指向等号,因为这是第一个无效标记。

{
text: str = ''

还有例外:

  File ".code.tio", line 2
    text: str = ''
              ^
SyntaxError: invalid syntax

【讨论】:

  • 感谢您的详细解释。请注意,我在问题中添加了以下关于我的情况的重要见解:另请注意,当我在 Visual Studio 代码中运行此代码时,在终端中运行而不是 CTRL-ALT-N,则选择的 python 版本为 3.6,并且运行良好没有任何语法错误,所以我认为代码运行器没有看到我在选择 >python: 选择解释器时看到的相同 python 版本(问题已使用这些详细信息更新)。
  • 由于可能有多个可能的答案,我将留下我的答案。尽管它可能对您没有帮助,但它可能会帮助遇到我描述的问题的人。太好了,您自己找到了原因。
猜你喜欢
  • 2012-12-27
  • 1970-01-01
  • 2015-09-08
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 2013-12-19
  • 2020-07-30
  • 1970-01-01
相关资源
最近更新 更多