Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does VSCode Code Runner not support f-strings?

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.

like image 996
Bytes2048 Avatar asked Oct 27 '25 12:10

Bytes2048


2 Answers

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

like image 73
Steven Fraser Avatar answered Oct 30 '25 01:10

Steven Fraser


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,
like image 39
XAMES3 Avatar answered Oct 30 '25 02:10

XAMES3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!