I have installed visual studio code and code runner
extension. I then have this piece of code:
text: str = "slkdfjsd"
I click CTRL-ALT-N
and I get:
text: str = "slkdfjsd"
^
SyntaxError: invalid syntax
I like using types, and this program worked, it looks like its complaining about the types how can I have it understand that types are ok?
more details:
$ /usr/bin/env python3 --version
Python 3.6.6 :: Anaconda, Inc.
and when it's running it with:
[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
Code runner plugin docs says:
$pythonPath
: The path of Python interpreter (set byPython: Select Interpreter command
)
but when I run print the path as comments suggested, I get a different version:
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
which is different as you can see above from the >python: select interpreter
which I have chosen.
Note also that when I run this code in visual studio code with run in terminal instead of CTRL-ALT-N
then the python version chosen is 3.6 and it runs fine without any syntax error, so it I think it's something with the code runner not seeing the same python version that I see when selecting >python: select interpreter
Update: I see indeed that code-runner is using the wrong python interpreter as stated above, so I opened my user settings and tried to update python
to point to the correct interpreter however it did not change anything it's still using the same wrong interpreter here is what I tried:
{
"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"
}
}
However also after changing that (maybe i'm doig something wrong i'm vscode newbie) I still see that the code-runner is running it with:
[Running] /usr/bin/env python3 "/home/myuser/dev/projects/python-snippets/text-summarization"
Note: the following answer assumes you are on the correct version (3.6+), if not: simply, variable annotation isn't supported on your current version of Python.
The problem may seem like the type annotation is causing the SyntaxError
, but another very plausible possibility is that there is an unclosed parenthesis or a unclosed something in the lines preceding. Since in the docs, it said:
The parser repeats the offending line and displays a little ‘arrow’ pointing at the earliest point in the line where the error was detected. The error is caused by (or at least detected at) the token preceding the arrow
(emphasis mine)
The parser can only detect an unclosed bracket when a token that is invalid in that context is given. Since brackets and parenthesis can carry through multiple lines (which means EOL wouldn't be raised), and text
is a valid variable identifier, which leaves only that colons aren't allowed in brackets or parentheses (excepted when it's used as an parameter, which also accepts type annotation).
Here's an reproducible example of your code hosted on tio.run (the SE code-golf compiler):
https://tio.run/##K6gsycjPM/7/X4OrJLWixEqhuKRIwVZBXf3/fwA
(
text: str = ''
The :
is the first invalid token in that context.
File ".code.tio", line 2
text: str = ''
^
SyntaxError: invalid syntax
If you have an unclosed dict instead, where colons are allowed, the arrow would be pointing somewhere else since text: str
are all valid tokens proceeded by an opening {
. The pointer will be pointing at the equal sign, since that's the first invalid token.
{
text: str = ''
And the exception:
File ".code.tio", line 2
text: str = ''
^
SyntaxError: invalid syntax
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With