4

I have this issue with VSCode and the Code Runner extension and here is the code snippet causing the issue:

class init_error(Exception):
    def __init__(self, comp="Internals"):
        self.component = comp
        self.message = f"Error occurred while initiating {self.component}"

    def __str__(self):
        return self.message

I first thought it was mistaking the compiler between Python2 and Python3, but after specifying #!/usr/bin/env python3 and checking that print("foo") works, I was fairly certain that it was not a version issue. I have checked that the compiler in Code Runner is set to 3.7.4 64-bit so I tried running the code through the Python extension, and it worked, so I believe this is a Code Runner issue.

Sorry for being long, but lastly, the f-strings are NOT underlined in red and it does not come up as a problem, therefore for some reason, f-strings are considered to be valid syntax, but does not run in only the Code Runner extension.

How can I make Code Runner accept f-strings?

Thank you for the help.

P.S. I don't think this is relevant, but I could have sworn it worked a month ago.

Bytes2048
  • 344
  • 1
  • 6
  • 16
  • 1
    "does not run" -- that's your interpretation, but what did you observe? Also read [ask] and extract a [mcve]. Further, which version are you running? You can find out the actual Python version from inside Python! – Ulrich Eckhardt Sep 30 '19 at 11:35
  • have you checked the python interpreter uses by VSCode was python3.6 or later? – Saleem Ali Sep 30 '19 at 11:51
  • 1
    To follow up with what @beer44 suggested, run `import sys; print(sys.version_info)` to see what version of Python is being run. – Brett Cannon Sep 30 '19 at 23:43
  • Ok, I found something wierd: `print(sys.version)` first gave me 2.7.10, but I was using `print()` without any problems. When I added `#!/usr/bin/env python3` I got 3.7.4, and the f-strings worked fine. So thank you... – Bytes2048 Oct 01 '19 at 00:00

3 Answers3

5

Below are links to the steps I took to get f-strings to work in vs-Code

  1. Search for Code-Runner in Extensions and click on the gear icon 'manage', in the dropdown menu click on Extension Settings

  1. Scroll down to Code-runner Executor Map and click Edit in settings.json

  1. On line 67 change python -u to python3 -u

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
Steven Fraser
  • 51
  • 2
  • 4
2

Try adding this is settings.json:

"code-runner.executorMap": {
        "python": "$pythonPath -u $fullFileName"
    },
"code-runner.clearPreviousOutput": true,
"code-runner.showExecutionMessage": false,
"code-runner.ignoreSelection": true,
"code-runner.saveFileBeforeRun": true,
XAMES3
  • 147
  • 1
  • 6
1

This is happening to me also when i use within vscode terminal:

python filename.py

However using below, it works:

python3 filename.py 
Snowcat
  • 470
  • 8
  • 16