Reputation: 11
I was progressing through the exercises on Zed Shaw's Learn Python 3 the Hard Way but then on Exercise 6, I encountered a syntax error, which I am not able to figure out. I tried searching on Google, StackOverflow (other posts), but none of the solutions mentioned work for me.
The snippet of the code that is throwing this error is:
types_of_people = 10
x = f"There are {types_of_people} types of people."
print(x)
I am using Visual Studio Code 1.38.1, with Python 3.7.4 64-bit on macOS Mojave 10.14.6.
Surprisingly, I executed the code three different ways, and two of the methods showed the same error, but a third method actually executed the code successfully. I am trying to understand why VSCode is not able to execute the python script. Any help would be highly appreciated.
Used the standard way to execute python script in VSCode:
This method gave the SyntaxError. The error output is:
[Running] python -u "/Users/e139177/Documents/Programming/Learn-Programming/tempCodeRunnerFile.py"
File "/Users/e139177/Documents/Programming/Learn-Programming/tempCodeRunnerFile.py", line 2
x = f"There are {types_of_people} types of people."
SyntaxError: invalid syntax
[Done] exited with code=1 in 0.035 seconds
Screenshot 1 shows the error in VS Code.
Used "Run Python file in terminal" option in VS Code.
This method executed the script successfully, and the output generated is:
KENMACC02XG4AEJHD2:Learn-Programminge139177$/usr/local/bin/python3/Users/e139177/Documents/Programming/Learn-Programming/Exercise6.py
There are 10 types of people.
KENMACC02XG4AEJHD2:Learn-Programming e139177$
Screenshot 2 shows the successfully executed script in VS Code terminal.
Method 3
Used MacOS terminal to directly execute the python script, without using VSCode.
This method also gave the same SyntaxError. The error output is:
KENMACC02XG4AEJHD2:Learn-Programming e139177$ python Exercise6.py
File "Exercise6.py", line 2
x = f"There are {types_of_people} types of people."
^
SyntaxError: invalid syntax
KENMACC02XG4AEJHD2:Learn-Programming e139177$
Screenshot 3 shows the successfully executed script in VS Code terminal.
I am not sure why the script executes successfully when run within the VSCode terminal, but it does not do so when executed using VSCode's "Run" command, or when executed within the macOS terminal directly.
Upvotes: 1
Views: 4127
Reputation: 29598
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.
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 executorPATH
. e.g. To set the executorPATH
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/[email protected]/bin/python3 -u",
In the example above, I set it to the Python3.8 installed with Homebrew:
$ brew info [email protected]
...
Python has been installed as
/usr/local/opt/[email protected]/bin/python3
Then check that Code Runner now uses the correct path:
[Running] /usr/local/opt/[email protected]/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.
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/[email protected]/bin/python3
Q$ alias python3.8=/usr/local/opt/[email protected]/bin/python3
Q$ alias python3.9=/usr/local/opt/[email protected]/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.
Upvotes: 2
Reputation: 16030
In Method 1 you are using the Code Runner extension, not the Python extension and so it is simply using python
and not the Python interpreter you have selected for the Python extension. Method 3 fails because python
is traditionally Python 2 unless you have an activated virtual environment and macOS only comes with Python 2 installed by default.
To solve Method 1 you will have to set up Code Runner appropriately. for Method 3 you can use a virtual environment for Python 3 and when that's activated you will get what you expect when running python
.
Upvotes: 2