【发布时间】: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