The main problem with Method 1 and Method 3 is that you are using Python 2.7 to run your app, and f-strings are only available starting on Python 3.6.
Basically, you need to make sure you are using a Python 3.6+ interpreter. The answer from Brett Cannon pretty much summarizes how to do that, but here is the expanded form of that answer.
Method 1: Code Runner
The "[Running]"
part of the terminal output indicates you are using Code Runner (notice the screenshots of the extension page also show "[Running]"
). Code Runner has an Executor Map, which sets how it runs the code for different languages:
Configuration
Make sure the executor PATH of each language is set in the environment
variable. You could also add entry into code-runner.executorMap
to set
the executor PATH
. e.g. To set the executor PATH
for ruby, php and
HTML:
{
"code-runner.executorMap": {
"javascript": "node",
"php": "C:\\php\\php.exe",
"python": "python",
"perl": "perl",
"ruby": "C:\\Ruby23-x64\\bin\\ruby.exe",
"go": "go run",
"html": "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
}
}
By default, it just uses python
, which, depending on your system, can be Python 2 or some other version that is not the correct version you want to use.
You'll need to change it in the settings.

On clicking "Edit in settings.json", find the entry for python
:
"code-runner.executorMap": {
...
"python": "python -u",
...
And change it to the correct Python version:
"code-runner.executorMap": {
...
"python": "/usr/local/opt/python@3.8/bin/python3 -u",
In the example above, I set it to the Python3.8 installed with Homebrew:
$ brew info python@3.8
...
Python has been installed as
/usr/local/opt/python@3.8/bin/python3
Then check that Code Runner now uses the correct path:
[Running] /usr/local/opt/python@3.8/bin/python3 -u "/path/to/test.py"
There are 10 types of people.
Used the standard way to execute python script in VSCode
Actually, using Code Runner isn't the "standard way". You don't actually need it. The VS Code docs on Python in Visual Studio Code does not tell you to install it. As long as you select the correct Python environment, which you can check from the status bar at the bottom, you can just click on the green play button or use "Run Python File in Terminal", and it should use the correct version.

It's the reason why Method 2 ran successfully.
Method 3: From Terminal
I am using ... with Python 3.7.4 64-bit on MacOS Mojave 10.14.6.
Mac OS comes with Python 2.7 as a built-in, and it is the one used when you use python
:
Q$ python -V
Python 2.7.16
Q$ python test.py
File "test.py", line 2
x = f"There are {types_of_people} types of people."
^
SyntaxError: invalid syntax
You need to use python3
or whichever is the correct interpreter depending on you how installed Python 3.x. If you are Homebrew's Python 3.x:
The executables are organised as follows:
python3
points to Homebrew’s Python 3.x (if installed)
pip3
points to Homebrew’s Python 3.x’s pip (if installed)
If you have multiple versions, it's a good idea to alias them:
Q$ alias python3.7=/usr/local/opt/python@3.7/bin/python3
Q$ alias python3.8=/usr/local/opt/python@3.8/bin/python3
Q$ alias python3.9=/usr/local/opt/python@3.9/bin/python3
Q$ python3.7 -V
Python 3.7.9
Q$ python3.8 -V
Python 3.8.6
Q$ python3.9 -V
Python 3.9.1
Q$ python3.7 test.py
There are 10 types of people.
Q$ python3.8 test.py
There are 10 types of people.
Q$ python3.9 test.py
There are 10 types of people.
Using virtual environments is also a best practice approach:
Q$ python3.8 -m venv myvenv
Q$ source ./myvenv/bin/activate
(myvenv) Q$ python -V
Python 3.8.6
(myvenv) Q$ python test.py
There are 10 types of people.