8

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 by Python: 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"
9
  • 1
    Looks like you are running the code on an older Python version. Commented Jul 6, 2018 at 5:47
  • 1
    sounds like it but i don't get why - it's saying the exact python its running its /usr/bin/env python3 as mentioned and the version of it is: ` 3.6.6` as mentioned in the quesion. Commented Jul 6, 2018 at 5:48
  • 1
    Can you confirm that from sys.version_info? Commented Jul 6, 2018 at 5:53
  • 1
    hmm i got: [Running] /usr/bin/env python3 "/home/user/dev/projects/python-snippets/text-summarization.py" sys.version_info: sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0), i was choosing the python interpreter with ctrl-shift-p and then >python: select interperter looks like the code runner plugin takes it from a different place :( Commented Jul 6, 2018 at 5:58
  • 1
    So there are few things you need to notice. The terminal you launch inside doesn't know the selected interpreter, so it will use the default python. Also I remember seeing another issue which mentioned that an extension currently cannot fetch selected interpreter information, which means your extension may have an issue. Best thing is to use absolute path of the python which you wish to run. Also if you have virtual environment then terminal python cannot not use the environment python by default, you need to manually source it Commented Jul 8, 2018 at 11:05

1 Answer 1

6

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
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the detailed explanation. please note I have added the following important insights about my situation to the question: 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 (question has been updated with these details).
Since there's possibly multiple possible answers, I'll leave mine to be. Although it might not have helped you, it might help someone who's experiencing the problem that I described. Great that you found the reason yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.